Tkinter 入口和按钮组合功能在其他功能中不起作用
Tkinter entry and button combined function doesn't work in other function
我创建了一个调用具有多个条目和一个按钮的 GUI 的函数。
如果我按下按钮,条目上的值将在函数上 returned。
def date_time_gui ():
win = tk.Tk()
win.title('powered by Python') # win title
win.geometry('700x400') # size of win
# add label on the win
label = tk.Label(win,
text='type the heaving datetime and out water datetime',
font = ('Arial Bold',25)
)
label.place(relx = 0.5, rely = 0.05, anchor = 'n')
# label and entry for heaving datetime
heaving_label = tk.Label(win,
text='type heaving datetime',
font = ('Arial Bold', 15)
)
heaving_label.place(relx = 0.1, rely = 0.3, anchor = 'w')
h_year_label = tk.Label(win, text='YEAR (4 digits) :')
h_year_label.place(relx = 0.1, rely = 0.4, anchor = 'w')
h_year = tk.Entry(win, fg='black', width = 5)
h_year.place(relx = 0.3, rely = 0.4, anchor = 'w')
h_month_label = tk.Label(win, text='MONTH (2 digits) :')
h_month_label.place(relx = 0.1, rely = 0.5, anchor = 'w')
h_month = tk.Entry(win, fg='black', width = 5)
h_month.place(relx = 0.3, rely = 0.5, anchor = 'w')
h_date_label = tk.Label(win, text='DATE (2 digits) :')
h_date_label.place(relx = 0.1, rely = 0.6, anchor = 'w')
h_date = tk.Entry(win, fg='black', width = 5)
h_date.place(relx = 0.3, rely = 0.6, anchor = 'w')
h_hour_label = tk.Label(win, text='HOUR (24h, 2 digits) :')
h_hour_label.place(relx = 0.1, rely = 0.7, anchor = 'w')
h_hour = tk.Entry(win, fg='black', width = 5)
h_hour.place(relx = 0.3, rely = 0.7, anchor = 'w')
h_minute_label = tk.Label(win, text='MINUTE (2 digits) :')
h_minute_label.place(relx = 0.1, rely = 0.8, anchor = 'w')
h_minute = tk.Entry(win, fg='black', width = 5)
h_minute.place(relx = 0.3, rely = 0.8, anchor = 'w')
# label and entry for out water datetime
outwater_label = tk.Label(win,
text='type out water datetime',
font = ('Arial Bold', 15)
)
outwater_label.place(relx = 0.5, rely = 0.3, anchor = 'w')
o_year_label = tk.Label(win, text='YEAR (4 digits) :')
o_year_label.place(relx = 0.5, rely = 0.4, anchor = 'w')
o_year = tk.Entry(win, fg='black', width = 5)
o_year.place(relx = 0.7, rely = 0.4, anchor = 'w')
o_month_label = tk.Label(win, text='MONTH (2 digits) :')
o_month_label.place(relx = 0.5, rely = 0.5, anchor = 'w')
o_month = tk.Entry(win, fg='black', width = 5)
o_month.place(relx = 0.7, rely = 0.5, anchor = 'w')
o_date_label = tk.Label(win, text='DATE (2 digits) :')
o_date_label.place(relx = 0.5, rely = 0.6, anchor = 'w')
o_date = tk.Entry(win, fg='black', width = 5)
o_date.place(relx = 0.7, rely = 0.6, anchor = 'w')
o_hour_label = tk.Label(win, text='HOUR (24h, 2 digits) :')
o_hour_label.place(relx = 0.5, rely = 0.7, anchor = 'w')
o_hour = tk.Entry(win, fg='black', width = 5)
o_hour.place(relx = 0.7, rely = 0.7, anchor = 'w')
o_minute_label = tk.Label(win, text='MINUTE (2 digits) :')
o_minute_label.place(relx = 0.5, rely = 0.8, anchor = 'w')
o_minute = tk.Entry(win, fg='black', width = 5)
o_minute.place(relx = 0.7, rely = 0.8, anchor = 'w')
def run_func ():
global heaving
global outwater
heaving = h_year.get()+'.'+h_month.get()+'.'+h_date.get()+' '+h_hour.get()+' '+h_minute.get()
outwater = o_year.get()+'.'+o_month.get()+'.'+o_date.get()+' '+o_hour.get()+' '+o_minute.get()
print('here')
win.destroy()
print('there')
return heaving, outwater
print('else')
button = ttk.Button(win, text = 'run', command = run_func)
button.place(relx = 0.8, rely = 0.9, anchor = 'w')
print('this')
win.mainloop()
print('where')
return heaving, outwater
如果我在同一个脚本中使用以下代码 运行 它就可以工作。
if __name__ == "__main__":
h, o = date_time_gui()
print(h,o)
它打印出 h, o 是函数 date_time_gui
的 return 值
但是,如果我将此函数应用于其他定义,例如
def other_def():
## some code
heaving_datetime, out_water_datetime = date_time_gui()
它只是停止工作。
我想检查date_time_gui中哪一行有问题,所以我打印了'there'、'where'等
当我运行 other_def时,它打印出来
别的
这个
这里
那里
然后没有打印出来'where'
我真的不知道是什么问题,因为 date_time_gui 函数本身运行良好。
当我将此应用于其他功能时,它不起作用。
- 也许,为了 运行 other_def 我制作了一个带有复选框的 gui 运行 other_def。
所以,如果我检查 gui 上的 other_def 功能并点击 运行 按钮,gui 将被破坏并且 other_def 将是 运行。 date_time_gui 的新 gui 弹出并点击 运行 按钮,然后 gui 销毁并停止工作。
使用 tkinter window
- 调用 loop() 方法会激活 tkinter 消息泵并将停止线性 python 代码,直到 window 关闭
- 您可以通过手动关闭 window 或从代码中调用 quit() 来退出循环。
在你的例子中,调用 win.mainloop()
停止了线性脚本。
您可以通过将 win.quit()
添加到 run_func
函数来退出循环。
试试这个代码:
def run_func ():
global heaving
global outwater
heaving = h_year.get()+'.'+h_month.get()+'.'+h_date.get()+' '+h_hour.get()+' '+h_minute.get()
outwater = o_year.get()+'.'+o_month.get()+'.'+o_date.get()+' '+o_hour.get()+' '+o_minute.get()
print('here')
win.destroy()
print('there')
win.quit() # exit gui loop
return heaving, outwater # not needed
print('else')
button = ttk.Button(win, text = 'run', command = run_func)
button.place(relx = 0.8, rely = 0.9, anchor = 'w')
print('this')
win.mainloop() # python code stops here until window closed
print('where')
heaving, outwater = 3,4 # for testing
return heaving, outwater
if __name__ == "__main__":
h, o = date_time_gui()
print(h,o)
输出
else
this
here
there
where
3 4
我创建了一个调用具有多个条目和一个按钮的 GUI 的函数。 如果我按下按钮,条目上的值将在函数上 returned。
def date_time_gui ():
win = tk.Tk()
win.title('powered by Python') # win title
win.geometry('700x400') # size of win
# add label on the win
label = tk.Label(win,
text='type the heaving datetime and out water datetime',
font = ('Arial Bold',25)
)
label.place(relx = 0.5, rely = 0.05, anchor = 'n')
# label and entry for heaving datetime
heaving_label = tk.Label(win,
text='type heaving datetime',
font = ('Arial Bold', 15)
)
heaving_label.place(relx = 0.1, rely = 0.3, anchor = 'w')
h_year_label = tk.Label(win, text='YEAR (4 digits) :')
h_year_label.place(relx = 0.1, rely = 0.4, anchor = 'w')
h_year = tk.Entry(win, fg='black', width = 5)
h_year.place(relx = 0.3, rely = 0.4, anchor = 'w')
h_month_label = tk.Label(win, text='MONTH (2 digits) :')
h_month_label.place(relx = 0.1, rely = 0.5, anchor = 'w')
h_month = tk.Entry(win, fg='black', width = 5)
h_month.place(relx = 0.3, rely = 0.5, anchor = 'w')
h_date_label = tk.Label(win, text='DATE (2 digits) :')
h_date_label.place(relx = 0.1, rely = 0.6, anchor = 'w')
h_date = tk.Entry(win, fg='black', width = 5)
h_date.place(relx = 0.3, rely = 0.6, anchor = 'w')
h_hour_label = tk.Label(win, text='HOUR (24h, 2 digits) :')
h_hour_label.place(relx = 0.1, rely = 0.7, anchor = 'w')
h_hour = tk.Entry(win, fg='black', width = 5)
h_hour.place(relx = 0.3, rely = 0.7, anchor = 'w')
h_minute_label = tk.Label(win, text='MINUTE (2 digits) :')
h_minute_label.place(relx = 0.1, rely = 0.8, anchor = 'w')
h_minute = tk.Entry(win, fg='black', width = 5)
h_minute.place(relx = 0.3, rely = 0.8, anchor = 'w')
# label and entry for out water datetime
outwater_label = tk.Label(win,
text='type out water datetime',
font = ('Arial Bold', 15)
)
outwater_label.place(relx = 0.5, rely = 0.3, anchor = 'w')
o_year_label = tk.Label(win, text='YEAR (4 digits) :')
o_year_label.place(relx = 0.5, rely = 0.4, anchor = 'w')
o_year = tk.Entry(win, fg='black', width = 5)
o_year.place(relx = 0.7, rely = 0.4, anchor = 'w')
o_month_label = tk.Label(win, text='MONTH (2 digits) :')
o_month_label.place(relx = 0.5, rely = 0.5, anchor = 'w')
o_month = tk.Entry(win, fg='black', width = 5)
o_month.place(relx = 0.7, rely = 0.5, anchor = 'w')
o_date_label = tk.Label(win, text='DATE (2 digits) :')
o_date_label.place(relx = 0.5, rely = 0.6, anchor = 'w')
o_date = tk.Entry(win, fg='black', width = 5)
o_date.place(relx = 0.7, rely = 0.6, anchor = 'w')
o_hour_label = tk.Label(win, text='HOUR (24h, 2 digits) :')
o_hour_label.place(relx = 0.5, rely = 0.7, anchor = 'w')
o_hour = tk.Entry(win, fg='black', width = 5)
o_hour.place(relx = 0.7, rely = 0.7, anchor = 'w')
o_minute_label = tk.Label(win, text='MINUTE (2 digits) :')
o_minute_label.place(relx = 0.5, rely = 0.8, anchor = 'w')
o_minute = tk.Entry(win, fg='black', width = 5)
o_minute.place(relx = 0.7, rely = 0.8, anchor = 'w')
def run_func ():
global heaving
global outwater
heaving = h_year.get()+'.'+h_month.get()+'.'+h_date.get()+' '+h_hour.get()+' '+h_minute.get()
outwater = o_year.get()+'.'+o_month.get()+'.'+o_date.get()+' '+o_hour.get()+' '+o_minute.get()
print('here')
win.destroy()
print('there')
return heaving, outwater
print('else')
button = ttk.Button(win, text = 'run', command = run_func)
button.place(relx = 0.8, rely = 0.9, anchor = 'w')
print('this')
win.mainloop()
print('where')
return heaving, outwater
如果我在同一个脚本中使用以下代码 运行 它就可以工作。
if __name__ == "__main__":
h, o = date_time_gui()
print(h,o)
它打印出 h, o 是函数 date_time_gui
的 return 值但是,如果我将此函数应用于其他定义,例如
def other_def():
## some code
heaving_datetime, out_water_datetime = date_time_gui()
它只是停止工作。
我想检查date_time_gui中哪一行有问题,所以我打印了'there'、'where'等
当我运行 other_def时,它打印出来 别的 这个 这里 那里
然后没有打印出来'where'
我真的不知道是什么问题,因为 date_time_gui 函数本身运行良好。 当我将此应用于其他功能时,它不起作用。
- 也许,为了 运行 other_def 我制作了一个带有复选框的 gui 运行 other_def。 所以,如果我检查 gui 上的 other_def 功能并点击 运行 按钮,gui 将被破坏并且 other_def 将是 运行。 date_time_gui 的新 gui 弹出并点击 运行 按钮,然后 gui 销毁并停止工作。
使用 tkinter window
- 调用 loop() 方法会激活 tkinter 消息泵并将停止线性 python 代码,直到 window 关闭
- 您可以通过手动关闭 window 或从代码中调用 quit() 来退出循环。
在你的例子中,调用 win.mainloop()
停止了线性脚本。
您可以通过将 win.quit()
添加到 run_func
函数来退出循环。
试试这个代码:
def run_func ():
global heaving
global outwater
heaving = h_year.get()+'.'+h_month.get()+'.'+h_date.get()+' '+h_hour.get()+' '+h_minute.get()
outwater = o_year.get()+'.'+o_month.get()+'.'+o_date.get()+' '+o_hour.get()+' '+o_minute.get()
print('here')
win.destroy()
print('there')
win.quit() # exit gui loop
return heaving, outwater # not needed
print('else')
button = ttk.Button(win, text = 'run', command = run_func)
button.place(relx = 0.8, rely = 0.9, anchor = 'w')
print('this')
win.mainloop() # python code stops here until window closed
print('where')
heaving, outwater = 3,4 # for testing
return heaving, outwater
if __name__ == "__main__":
h, o = date_time_gui()
print(h,o)
输出
else
this
here
there
where
3 4