2

我在 python 2.7 中有这个代码,它使用它在 a 上tkinter创建Button一个Frame来打开文件。下面有一个Label。我正在尝试这样做,因此一旦打开文件,标签就会在 上打印路径“file1.name”或其他任何内容,Label如果您打开一个新文件,它将Label再次更改。

此外,我敢打赌,在函数之间移动数据的方法比我在这里使用的更好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()
4

2 回答 2

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)
于 2012-11-24T01:23:38.577 回答
0

@mgilson 解决了您的第一个问题。您的第二个问题,关于如何在不使用 globals 的情况下在函数之间传递参数:

您可能希望将变量存储为应用程序类的属性:

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

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()
于 2014-07-07T08:59:43.063 回答