我想在另一个模块中使用tkinter模块返回的值:
myInputField.py
from tkinter import Tk, Label, Entry, Button, Frame, mainloop, messagebox
master = Tk()
def result():
a = e.get()
return a
Label(master, text="Your Answer", font='Verdana 10').grid(row=0, column=1, sticky='w')
e = Entry(master, font='Verdana 12', width=120)
f = Frame(master)
f.grid(row=2, column=1)
e.grid(row=1, column=1)
Button(f, text='OK', command=result).pack(side='left')
Button(f, text='Quit', command=master.destroy).pack(side='left')
e.focus_set()
mainloop()
在另一个模块中调用:
import myInputField
print(a)
请帮助,谢谢。
答案 0 :(得分:0)
我可能会使用线程和队列,因为您基本上是在尝试同时运行两个程序元素。
类似的事情会起作用:
文件“ Test.py”:
import threading, queue, myInputField
class test():
def __init__(self):
q = queue.Queue()
t = threading.Thread(target = myInputField.popup, args = (q,))
t.start()
while t.is_alive():
print(q.get()) # Or put it in a variable, whatever is needed.
test()
文件“ myInputField.py”:
from tkinter import Tk, Label, Entry, Button, Frame, mainloop, messagebox
class popup():
def __init__(self, q):
self.q = q
master = Tk()
Label(master, text="Your Answer", font='Verdana 10').grid(row=0, column=1, sticky='w')
e = Entry(master, font='Verdana 12', width=120)
f = Frame(master)
f.grid(row=2, column=1)
e.grid(row=1, column=1)
Button(f, text='OK', command= lambda: self.result(e.get())).pack(side='left')
Button(f, text='Quit', command=master.destroy).pack(side='left')
e.focus_set()
mainloop()
def result(self, r):
self.q.put(r)
它使用队列将信息从一个模块传递到另一个模块。
设置应为:
文件夹
__init__.py
(空python文件,指示此文件夹包含python模块)
myInputField.py
test.py
然后运行test.py
/[^@\s]/gy