简体   繁体   中英

python 2.7 Tk change label to carry file name from opened file

I have this code in python 2.7 using tkinter that creates a Button on a Frame to open a file. There's a Label under it. I'm trying to make it so once the file is opened, the label prints the path, "file1.name" or whatever, on the Label , and if you open a new file it will change that Label again.

Also, I'm betting there's a better way to move data between the functions than I'm using here with global , but that's not worrying me right now.

I have to move the data from the opened files between functions so that I can mix the data and save to a new file. The code is:

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()

If I understand correctly, you want something like ( untested ):

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)

@mgilson has solved your first question. Your second question, about how to pass parameters between functions without using globals :

you might want to look at storing the variables as attributes on your application class :

The syntax self. is an attribute on the current instance (an instance being a particular example of a class - just like your car is a specific example of a class "car"). You can use instance attributes in this example as if they are 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM