Tkinter:在另一个文件中使用条目值

loading...


0

当file1.py运行并且点击提交按钮时,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")

这就是我想要做的事情但是没有运气 .

loading...

1回答

  • 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)
    
评论

暂时没有评论!