尝试更改由右上角按钮控制的TEntry1的状态而不是更改TEntry1的状态时,会创建一个新窗口,该窗口的TEntry1变灰,所有变量输入为空白
GUI由第页生成
页面导出为2个文件,我将这2个文件合并在一起
左边的窗口是根据需要运行的程序,按钮最初会说开始,然后单击它将切换到运行模式,并将文本更改为停止。它还应该灰显条目小部件,但会创建一个新窗口,其中条目小部件灰显
我认为disableentry()中出现了问题:如何调用属性
def disableentry():
print("disableentry() start")
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):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_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")
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="")
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")
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")
self.Button1.configure(activeforeground="#000000")
self.Button1.configure(background="#d9d9d9")
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('')
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)
self
def run(self):
guisetup.vp_start_gui()
class guicontrols():
def disableentry():
print("disableentry() start")
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()
这是您的代码的清理版本。我将与窗口相关的所有内容都放在一个类中:
App
,以便可以正确访问所有变量我还将最初在成吨的
config()
函数中的小部件参数作为小部件类本身的参数;这是更好的语法我保留了线程,并添加了一个函数,当用户单击按钮时重新启用该条目。我还将
self.running_status = tkinter.IntVar()
之后的self.running_status.set(0)
行更改为self.running_status.set(1)
。这修复了一个错误,当程序首次启动时,您必须在任何事情发生之前单击按钮两次如果有些事情不是你想要的,或者如果你有任何问题或遇到任何问题,我会很乐意帮助你
相关问题 更多 >
编程相关推荐