[英]How to return the value of a variable only when a button has been clicked?
我的代码:
def file_exists(f_name):
select = 0
def skip():
nonlocal select
select = 1
err_msg.destroy()
def overwrite():
nonlocal select
select = 2
err_msg.destroy()
def rename():
global select
select = 3
err_msg.destroy()
# Determine whether already existing zip member's name is a file or a folder
if f_name[-1] == "/":
target = "folder"
else:
target = "file"
# Display a warning message if a file or folder already exists
''' Create a custom message box with three buttons: skip, overwrite and rename. Depending
on the users change the value of the variable 'select' and close the child window'''
if select != 0:
return select
我知道使用非本地方法是邪恶的,但至少在此程序中,我必须继续使用程序方法。
问题是,无论我按下哪个按钮,当我调用此函数时,它都会立即通过并立即返回select
的初始值(即0)。 当我按下一个按钮时, select
的值将相应更改。
那么,如何仅在按下按钮后才能返回? 如您所见,我的第一个尝试是仅当select为!= 0
时才返回该值,但这不起作用。
感谢您的建议!
您可以使用.update()
函数来阻止而不冻结GUI。 基本上,您在循环中调用root.update()
,直到满足条件为止。 一个例子:
def block():
import Tkinter as tk
w= tk.Tk()
var= tk.IntVar()
def c1():
var.set(1)
b1= tk.Button(w, text='1', command=c1)
b1.grid()
def c2():
var.set(2)
b2= tk.Button(w, text='2', command=c2)
b2.grid()
while var.get()==0:
w.update()
w.destroy()
return var.get()
print(block())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.