當試圖改變由右上方的按鈕控制的TEntry1的狀態時,而不是改變TEntry1的狀態,它會創建一個新的視窗,其中TEntry1是灰色的,所有變數輸入都是空白的。
GUI是由PAGE生成的
。PAGE 輸出到 2 個檔案,我為這個問題合并了這 2 個檔案
。左邊的視窗是程式按要求運行,按鈕最初顯示為啟動,然后點擊它將切換到運行模式,并將文字改為停止。 它還應該使條目小部件變灰,但卻創建了一個新的視窗,條目小部件也變灰了
我相信這個問題是在一個新的視窗中出現的。
我相信問題是發生在disableentry()中:該屬性的呼叫方式
def disableentry()。
print("disableentry() start"/span>)
root = tk.Tk()
Toplevel1(root).TEntry1.configure(state='disabled')
import sys
import threading
import tkinter as tk
import tkinter.ttk as ttk
def destroy_Toplevel1()。
thread0.stop()
class Toplevel1:
def __init__(self, top=None)。
''該類配置并填充頂層視窗。
top是包含頂層的視窗。''/span>
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black' >。
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85' >。
_ana2color = '#ececec' # Closest X11 color: 'gray92'
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.', background=_bgcolor)
self.style.configure('.', foreground=_fgcolor)
self.style.configure('.', font="TkDefaultFont")
self.style.map('. ' ,background=
[('selected', _compcolor), ('active', _ana2color)])
top.geometry("460x406 660 210")
top.minsize(120, 1)
top.maxsize(3844, 1061)
top.resizable(0, 0)
top.title("PAGE GUI")
top.configure(background="#d9d9d9"/span>)
top.configure(highlightbackground="#d9d9d9")
top.configure(highlightcolor="black")
self.TEntry1 = ttk.Entry(top)
self.TEntry1.place(x=76, y=41, height=21, width=137)
self.TEntry1.configure(takefocus=""/span>)
self.TEntry1.configure(cursor="ibeam")
self.TEntry1.configure(textvariable=entry1text)
self.Label1 = tk.Label(top)
self.Label1.place(x=21, y=41, height=31, width=38)
self.Label1.configure(activebackground="#f9f9f9"/span>)
self.Label1.configure(activeforeground="black")
self.Label1.configure(background="#d9d9d9")
self.Label1.configure(disabledforeground="#a3a3a3")
self.Label1.configure(foreground="#000000")
self.Label1.configure(highlightbackground="#d9d9d9")
self.Label1.configure(highlightcolor="black")
self.Label1.configure(text=''IP 1'' )
self.Button1 = tk.Button(top)
self.Button1.place(x=330, y=41, height=24, width=107)
self.Button1.configure(activebackground="#ececec"/span>)
self.Button1.configure(activeforeground="#000000")
self.Button1.configure(background="#d9d9d9"/span>)
self.Button1.configure(disabledforeground="#a3a3a3")
self.Button1.configure(foreground="#000000")
self.Button1.configure(highlightbackground="#d9d9d9")
self.Button1.configure(highlightcolor="black")
self.Button1.configure(pady="0")
self.Button1.configure(textvariable=button_text)
self.Button1.configure(command=guicontrols.start_stop)
class guisetup()。
def set_Tk_var()。
global running_status
running_status = tk.IntVar()
running_status.set(0)
global button_text
button_text = tk.StringVar()
button_text.set('start')
global entry1text
entry1text = tk.StringVar()
entry1text.set(''/span>)
def vp_start_gui() 。
global val, w, root
root = tk.Tk()
guisetup.set_Tk_var()
top = Toplevel1 (root)
root.mainloop()
class guithread(threading.Thread)。
def __init__(self):
threading.Thread.__init__(self)
自己
def run(self)。
guisetup.vp_start_gui()
class guicontrols()。
def disableentry()。
print("disableentry() start"/span>)
root = tk.Tk()
Toplevel1(root).TEntry1.configure(state='disabled')
def start_stop() 。
print('PLA_support.start_stop')
if (running_status.get() == 1) 。
button_text.set("stop")
running_status.set(0)
guicontrols.disableentry()
else:
button_text.set("Start")
running_status.set(1)
sys.stdout.flush()
if __name__ == '__main__':
thread0 = guithread()
thread0.start()
uj5u.com熱心網友回復:
這是你的代碼的一個清理版本。我把所有與視窗有關的東西都放到一個類中。App,這樣所有的變數都可以被正確訪問。
import sys
import threading
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Tk)。
""主視窗。""。
def __init__(self):
tk.Tk.__init__(self)
# Set the theme
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black' >。
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85' >。
_ana2color = '#ececec' # Closest X11 color: 'gray92'
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('. ' , background=_bgcolor)
self.style.configure('. ', foreground=_fgcolor)
self.style.configure('. ', font="TkDefaultFont")
self.style.map(
'. ' ,
background=[('selected', _compcolor), ('active', _ana2color)]
)
self.geometry("460x406 660 210")
self.minsize(120, 1)
self.maxsize(3844, 1061)
self.resizable(0, 0)
self.title("PAGE GUI")
self.configure(background="#d9d9d9")
self.configure(highlightbackground="#d9d9d9")
self.configure(highlightcolor="black")
# Running statusset(1)
# 創建部件。
self.create_widgets()
def create_widgets(self)。
""創建部件并將其添加到視窗。""
self.entry1text = tk.StringVar()
self.entry1text.set("")
self.TEntry1 = ttk.Entry(
自身。
takefocus=""。
cursor="xterm"。
textvariable=self.entry1text
)
self.TEntry1.place(x=76, y=41, height=21, width=137)
# 標簽-
self.Label1 = tk.Label(
自身。
activebackground="#f9f9f9"/span>,
activeforeground="black",
background="#d9d9d9",
disabledforeground="#a3a3a3"。
foreground="#000000"。
highlightbackground="#d9d9d9",
highlightcolor="black",
text="IP 1".
)
self.Label1.place(x=21, y=41, height=31, width=38)
# 該按鈕set('start')
self.Button1 = tk.Button(
自身。
activebackground="#ececec",
activeforeground="#000000",
background="#d9d9d9",
disabledforeground="#a3a3a3"。
foreground="#000000"。
highlightbackground="#d9d9d9",
highlightcolor="black",
pady="0"。
textvariable=self.button_text,
command=self.start_stop
)
self.Button1.place(x=330, y=41, height=24, width=107)
def disable_entry(self)。
self.TEntry1.config(state="disabled")
def enable_entry(self)。
self.TEntry1.config(state=" enabled")
def start_stop(self)。
print('PLA_support.start_stop')
if (self.running_status.get() == 1) 。
self.button_text.set("Stop")
self.running_status.set(0)
self.disable_entry()
else:
self.button_text.set("Start")
self.running_status.set(1)
self.enable_entry()
class guithread(threading.Thread)。
def __init__(self):
threading.Thread.__init__(self)
self.app = App()
def run(self)。
self.app.mainloop()
if __name__ == '__main__'/span>:
thread0 = guithread()
thread0.run()
我還將原本在大量config()函式中的widget引數作為widget類本身的引數;這是更好的語法。
我保留了執行緒,并添加了一個函式,當用戶點擊按鈕時重新啟用該條目。我還將self.running_status.set(0)之后的一行self.running_status = tkinter.IntVar()改為self.running_status.set(1)。這修正了一個錯誤,即當程式第一次啟動時,你必須在任何事情發生之前點擊兩次按鈕。
如果有些東西不是你想要的,或者如果你有任何問題或遇到任何問題,我很樂意幫助你!
如果有些東西不是你想要的,或者如果你有任何問題或遇到任何問題,我很樂意幫助你。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/315121.html
標籤:

