[英]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.