2012-11-24 49 views
2

我在Python 2.7中使用tkinter创建了一个Button上的Button以打开文件。它下面有一个Label。我试图让它一旦打开文件,标签打印路径,“file1.name”或其他,在Label,如果你打开一个新文件,它会改变Label再次。python 2.7 Tk更改标签以从打开的文件中携带文件名

此外,我打赌有更好的方法来移动功能之间的数据比我在这里用global,但这并不担心我现在。

我必须在功能之间移动打开的文件中的数据,以便我可以混合数据并保存到新文件中。该代码是:

from Tkinter import * 
import Tkinter 
import tkFileDialog 
import tkMessageBox 
root = Tkinter.Tk() 
global rfile1 
global rfile2 
rfile1 = "" 
rfile2 = "" 
class Application(Frame): 

    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.grid() 
     self.createWidgets1() 
     self.createLabels1() 
     self.createWidgets2() 
     self.createLabels2() 

    def createWidgets1(self): 
     self.oButton = Button(self, text = "open1", command = self.openfile1) 
     self.oButton.grid() 

    def createLabels1(self): 
     self.oLabel = Label(self, text = "whoops") 
     self.oLabel.grid() 

    def createWidgets2(self): 
     self.oButton = Button(self, text = "open2", command= self.openfile2) 
     self.oButton.grid() 

    def createLabels2(self): 
     self.oLabel2 = Label(self, text = "whoops2") 
     self.oLabel2.grid() 

    def openfile1(self): 
     file1 = tkFileDialog.askopenfile(title = "choose a file, *****", parent=root, mode = 'rb') 

     rfile1 = file1.read() 
     tkMessageBox.showinfo("oy", rfile1, parent=root) 

    def openfile2(self): 
     file2 = tkFileDialog.askopenfile(parent=root, mode='rb') 

     rfile2 = file2.read() 
     tkMessageBox.showinfo("hola", rfile2, parent=root) 

app = Application() 
app.master.title("whiggy whompus") 
app.mainloop() 

回答

2

如果我理解正确的话,你要像(未经测试):

def openfile1(self): 
    file1 = tkFileDialog.askopenfile(title = "choose a file, *****", parent=root, mode = 'rb') 
    self.oLabel.configure(text=file1.name)  
    rfile1 = file1.read() 
    tkMessageBox.showinfo("oy", rfile1, parent=root) 
+0

绝对起作用!非常感谢 : ) –

0

@mgilson解决了第一个问题。你的第二个问题,关于如何传递函数之间的参数,而无需使用全局变量:

你可能想看看在存储上的应用程序类的变量属性:

语法的自我。是当前实例的一个属性(一个实例是一个类的特定示例 - 就像您的汽车是“汽车”类的具体示例一样)。 您可以在本例中使用实例属性,就好像它们是全局变量一样。

from Tkinter import * 
import Tkinter 
import tkFileDialog 
import tkMessageBox 
root = Tkinter.Tk() 

class Application(Frame): 

    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.grid() 
     self.createWidgets1() 
     self.createLabels1() 
     self.createWidgets2() 
     self.createLabels2() 
     self.rfile1 = "" 
     self.rfile2 = "" 

    def createWidgets1(self): 
     self.oButton = Button(self, text = "open1", command = self.openfile1) 
     self.oButton.grid() 

    def createLabels1(self): 
     self.oLabel = Label(self, text = "whoops") 
     self.oLabel.grid() 

    def createWidgets2(self): 
     self.oButton = Button(self, text = "open2", command= self.openfile2) 
     self.oButton.grid() 

    def createLabels2(self): 
     self.oLabel2 = Label(self, text = "whoops2") 
     self.oLabel2.grid() 

    def openfile1(self): 
     file1 = tkFileDialog.askopenfile(title = "choose a file, *****", parent=root, mode = 'rb') 

     self.rfile1 = file1.read() 
     tkMessageBox.showinfo("oy", self.rfile1, parent=root) 

    def openfile2(self): 
     file2 = tkFileDialog.askopenfile(parent=root, mode='rb') 

     self.rfile2 = file2.read() 
     tkMessageBox.showinfo("hola", self.rfile2, parent=root) 

app = Application() 
app.master.title("whiggy whompus") 
app.mainloop() 
相关问题