让我说我有这个代码
import math
from tkinter import *
def close_window():
root.destroy()
def fileName():
filename = content.get()
return filename;
root = Tk()
content = StringVar()
L2 = Label(root, text = "The Program").grid(row = 0, sticky = E)
L1 = Label(root, text = "Enter filename").grid(row = 1, column = 0, sticky = E)
E1 = Entry(root, bd = 5, textvariable = content).grid(row = 1, column = 1)
B1 = Button(root, text = "Ok", command = fileName).grid(row = 2, column = 0 )
B2 = Button(root, text = "Quit", command = close_window).grid(row = 2, column = 1)
root.mainloop()
print(fileName())
现在问题是我想存储我在E1中输入的内容(所以我以后可以做些事情)但是如何在GUI“外部”访问它?
我想要的程序是用户输入一个文件名,然后它在输入上运行一堆函数,然后根据给出的内容生成一个textmessage,但是我无法访问输入 文件名() 不回报任何东西。
答案 0 :(得分:0)
不确定这是否符合您的要求,但现在按钮点击打印,您将文件名变量设置为content.get()
import math
from tkinter import *
def close_window():
root.destroy()
def fileName():
filename = content.get()
return filename;
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
def prnt():
print(content.get())
root = Tk()
content = StringVar()
L2 = Label(root, text = "The Program").grid(row = 0, sticky = E)
L1 = Label(root, text = "Enter filename").grid(row = 1, column = 0, sticky = E)
E1 = Entry(root, bd = 5, textvariable = content).grid(row = 1, column = 1)
B1 = Button(root, text = "Ok", command = combine_funcs(fileName,prnt)).grid(row = 2, column = 0 )
B2 = Button(root, text = "Quit", command = close_window).grid(row = 2, column = 1)
root.mainloop()
print(fileName())