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

前端 未结 2 1423
清酒与你
清酒与你 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条回答
  •  梦毁少年i
    2021-01-15 04:37

    Generally, it is bad practice to use global variables to pass information around your program. However, if you really must do this, use a mutable data type (such as a list or a dict) as your global variable and change its contents from your callback function, main.

    returned_values = {}    # Create an empty dict.
    def main():
        returned_values['filename'] = askopenfilename(filetypes=[("Jpeg","*.jpg")])
        # returned_values['filename'] may now be accessed in the global scope.
    

    If you intend to do this frequently, consider implementing your own class to pass information around.

提交回复
热议问题