我已經嘗試了下面的代碼來阻止一個按鈕,因為如果在 def start 作業時點擊它,它會破壞整個應用程式。該按鈕同時呼叫 defs start 和 block,盡管函式 start 根本不起作用。
問題是我不能button_start.config(state=tk.DISABLED)輸入def start():,因為它每 1000 毫秒改變一次,而且按鈕奇怪地脈動。
我已經搜索過了,這就是我處理它的想法。我不是專業編碼員,這可能很愚蠢,所以我指望你的經驗。
root = tk.Tk()
def stop_app():
button_start.config(state=tk.NORMAL)
def start():
#something working here...
root.after(1000, start)
def block():
button_start.config(state=tk.DISABLED)
button_start = tk.Button( root, command=start and block)
button_start.place(x=250, y=235)
button_stop = tk.Button(root, command=stop_app)
button_stop.place(x=305, y=235)
uj5u.com熱心網友回復:
我想你可能把事情搞得太復雜了。下面的代碼只是Button根據第一個的狀態切換兩個s 的狀態,因此它們總是彼此相反。
import tkinter as tk
root = tk.Tk()
root.geometry('350x350')
def start_app():
if button_start.cget('state') == tk.NORMAL:
button_start.config(state=tk.DISABLED)
button_stop.config(state=tk.NORMAL)
def stop_app():
if button_stop.cget('state') == tk.NORMAL:
button_stop.config(state=tk.DISABLED)
button_start.config(state=tk.NORMAL)
button_start = tk.Button(root, text='Start', command=start_app, state=tk.NORMAL)
button_start.place(x=250, y=235)
button_stop = tk.Button(root, text='Stop', command=stop_app, state=tk.DISABLED)
button_stop.place(x=305, y=235)
root.mainloop()
單擊Start按鈕之前和單擊之后的螢屏截圖:

uj5u.com熱心網友回復:
線,
button_start = tk.Button(root, command=start and block)
有錯誤,command=start and block。同一個按鈕不能有 2 個功能。
你可以做的是:
def block():
button_start.config(state=tk.DISABLED)
def start():
block()
或者同樣的事情,但行數較少:
def start():
button_start.config(tk.DISABLED)
并將錯誤行更改為:
button_start = tk.Button(root, command=start)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/358530.html
