运行file1.py并单击Submit按钮时,submit方法将返回entry.get()值,这很好。但是,如果我也想在单击“提交”按钮时将entry.get()也转移到另一个python文件中。这可能吗?
file1.py
from tkinter import *
root = Tk()
def submit():
return entry.get()
entry = Entry(root)
entry.pack()
submit_button = Button(root, text = "Submit", command = submit)
submit_button.pack()
root.mainloop()
file2.py
import file1 as f1
from tkinter import messagebox
var = submit()
if var == "test":
messagebox.showinfo("Hello")
这是我想要做的,但是运气不好。
答案 0 :(得分:2)
您可以将值传递给您在另一个文件中定义的函数,像这样。
p1.py
from tkinter import *
from p2 import new_print
root = Tk()
def submit():
new_print(entry.get())
entry = Entry(root)
entry.pack()
submit_button = Button(root, text = "Submit", command = submit)
submit_button.pack()
root.mainloop()
p2.py
value_from_p1 = None
def new_print(value):
global value_from_p1
value_from_p1 = value
print(value)