Returning a value after calling a function with a button in Tkinter

前端 未结 2 1458
清酒与你
清酒与你 2021-01-15 04:34
from Tkinter import *
from tkFileDialog import askopenfilename
from PIL import Image
def main():
    filename = askopenfilename(filetypes=[(\"Jpeg\",\"*.jpg\")])
ret         


        
2条回答
  •  不要未来只要你来
    2021-01-15 04:42

    If you use the class based approach to Tk applications, instead of returning values from event handlers, you can assign them to instance variables. This the best approach, as function-based GUI applications don't scale well precisely because the need to place stuff at module scope.

    from Tkinter import *
    
    class Application(Frame):
    
        def main(self):
            self.filename = askopenfilename(filetypes=[("Jpeg","*.jpg")])
    
        def createWidgets(self):
            self.button = Button(root,text="Open",command=self.main)
            self.button.pack()
    
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.filename = None
            self.pack()
            self.createWidgets()
    
    root = Tk()
    root.title("Image Manipulation Program")
    app = Application(master=root)
    app.mainloop()
    

提交回复
热议问题