简体   繁体   中英

Python3 and tkinter GUI button to open a file

I have created a program that uses PIL to examine an image and return the size, amount of colors, dpi, and so on, but now I want to put my code into a GUI system to help the user.

I have used askopenfilename() in a function, but I am running into an issue when trying to open a new file. I have the function run after the program starts and it lets me select a file and works just fine. When I click the button to open a new file, it lets me select a new file but it does not change any of the displayed information.

How can I refresh the screen with new information after a new file is selected? Here is the code I have:

def openPattern():
    global fileName
    path = askopenfilename()
    fileOpen = open(path, 'r')
    fileName = os.path.basename(path)

if __name__ == '__main__':
    root = Tk()
    root.title("Art Intake | Developer Build")
    ms = MainScreen(root)
    ms.config(bg="grey")

    openPattern()
    pattern = Button(ms, text="Choose a file", command=openPattern,   
                highlightbackground='grey')
    pattern.pack()
    pName = Label(ms, text="Pattern Name: " + str(fileName), 
                 bg='grey')
    pName.pack()

    read = Button(ms, text="ReadMe", command=openRM, 
              highlightbackground='grey')
    read.place(rely=1.0, relx=1.0, x=-25, y=-15, anchor=SE)

    quit = Button(ms, text="Quit", command=ms.quit, 
             highlightbackground='grey')
    quit.place(rely=1.0, relx=1.0, x=-25, y=-45, anchor=SE)

root.mainloop()

At start you run openPattern() before label pName is created and label has fileName to display. But later you have to manually change text in label.

    pName['text'] = "Pattern Name: " + fileName

First I create empty label and later I run openPattern() so it can update text in existing label. When I click button then openPattern() does the same - it updates text in existing label.

(not tested because code was incomplet)

def openPattern():
    path = askopenfilename()
    fileOpen = open(path, 'r')
    fileName = os.path.basename(path)

    pName['text'] = "Pattern Name: " + fileName

if __name__ == '__main__':
    root = Tk()
    root.title("Art Intake | Developer Build")

    ms = MainScreen(root)
    ms.config(bg="grey")

    pattern = Button(ms, text="Choose a file", command=openPattern,   
                highlightbackground='grey')
    pattern.pack()
    pName = Label(ms, bg='grey')
    pName.pack()

    read = Button(ms, text="ReadMe", command=openRM, 
              highlightbackground='grey')
    read.place(rely=1.0, relx=1.0, x=-25, y=-15, anchor=SE)

    quit = Button(ms, text="Quit", command=ms.quit, 
             highlightbackground='grey')
    quit.place(rely=1.0, relx=1.0, x=-25, y=-45, anchor=SE)

    openPattern()

    root.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