python - Tkinter 标签未跨类更新(另一个)
问题描述
目标:跨类显示变量
问题:更新标签,特别是第 99 行
错误:仅逻辑
环境: Python 3.6.9,Tk 8.6.8,操作系统 Ubuntu 18.04
写上去:
我试图在包含代码的第 99 行pPanelSettings.cutLength
的pViewSettings
类中显示变量,但是我无法让它更新/刷新。包含textvariable
在我的任何标签中都会使其显示为空白,即使我给了它们一个初始值。在包含的尝试中,我留下了一个无效的刷新命令(第 106 行)以及一个无效的跟踪尝试(第 107 行)。其他尝试包括:
update_idletasks
,创建一个额外的字符串变量,它只是重新格式化整数变量只是为了确认这不是问题,相互引用pViewSettings
和pPanelSettings
内部(导致循环),以及我现在忘记的其他一些。
我最感兴趣的是找出我遗漏的任何东西的“原因”,因为我将来会再次重复这个过程。很抱歉用另一个新手标签问题填满论坛,但我自己在这一点上不知所措。我相信这是一个我完全想念的简单修复。
理想情况下,我想了解我哪里出错了textvariable
,因为这似乎最有可能是简单的解决方案,但与在线示例相比,我仍然没有看到我错在哪里。
我:
直到明年年初,我才计划进行 GUI 学习,但是我的这个项目的时间表加快了,所以我不得不快速投入。
我经历过的帖子:
https://yagisanatode.com/2018/02/26/how-to-display-and-entry-in-a-label-tkinter-python-3/
许多其他人,但只是丢失了我所有的书签。
代码:
import tkinter as tk
import time as time
import math as math
#create main window
root = tk.Tk()
root.title('machine')
#create main container
mainFrame = tk.Frame(root)
#layout main container, grow with window size
mainFrame.pack(fill=tk.BOTH, expand=True)
#create globals
inchesInFeet = tk.IntVar()
answerReset = tk.IntVar()
#set globals
answerReset.set(0)
#create classes
class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()
class pPanelSettings(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
#create variables
self.cutFeet = tk.IntVar()
self.cutInches = tk.IntVar()
self.cutLength = tk.IntVar()
self.panelCutConfirm = tk.BooleanVar()
#set variables
self.cutFeet.set(0)
self.cutInches.set(0)
self.cutLength.set(0)
self.panelCutConfirm.set(0)
#widgets
self.pageTitle = tk.Label(self, text='PANEL SETTINGS')
self.panelBlock = tk.LabelFrame(self, padx=5, pady=5)
self.panelLabel = tk.Label(self.panelBlock, text='CURRENT PANEL SET LENGTH')
self.panelSetting = tk.Label(self.panelBlock, text=self.cutLength.get())
self.messageDisplay = tk.Label(self, text='Set cut length (feet):')
self.wYesNo = tk.LabelFrame(self, padx=5, pady=5)
self.bYes = tk.Button(self.wYesNo, text='ENTER', command=self.setCutFeet, height=4, activebackground='green1', background='green2')
self.response = tk.Entry(self, justify='center')
#layout
self.pageTitle.pack(side='top', fill='x')
self.panelBlock.pack(side='top', fill='x', pady=5)
self.panelLabel.pack(side='left', fill='x', padx=50)
self.panelSetting.pack(side='right', fill='x')
self.messageDisplay.pack(side='top', fill='x', pady=60)
self.wYesNo.pack(side='bottom', fill='x', padx=20, pady=20, expand=True)
self.bYes.pack(side='left', fill='both', padx=10, expand=True)
self.response.pack(side='bottom', fill='x', padx=20, pady=60)
#settings
self.response.focus_set()
def clearText(self):
self.response.delete(0, 'end')
def transition(self):
self.after(2000, self.pageReset)
def pageReset(self): #resets widgets to initial view
self.response.delete(0, 'end')
self.messageDisplay.config(text='Set cut length (feet):')
self.response.pack(side='bottom', fill='x', padx=20, pady=60)
self.wYesNo.pack(side='bottom', fill='x', padx=20, pady=20, expand=True)
self.bYes.config(command=self.setCutFeet)
self.panelSetting.pack(side='right', fill='x')
def setCutFeet(self):
self.cutFeet.set(int(self.response.get()))
self.setCutConfirm()
def setCutConfirm(self):
self.panelCutConfirm.set(True)
self.cutLength.set(self.cutFeet.get())
self.panelSetting.config(text=self.cutLength.get())
print(self.cutLength.get()) ##verification test point using shell
self.messageDisplay.config(text='Panel cut length set.')
self.wYesNo.pack_forget()
self.transition()
class pViewSettings(Page): ##this page needs auto update/refresh to display current settings from other pages
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
#pages to objects
oPanelSettings = pPanelSettings(self)
#widgets
self.pageTitle = tk.Label(self, text='VIEW SETTINGS')
self.panelBlock = tk.LabelFrame(self, padx=5, pady=5)
self.panelLabel = tk.Label(self.panelBlock, text='PANEL LENGTH')
self.panelSetting = tk.Label(self.panelBlock, text=oPanelSettings.cutLength.get())
#layout
self.pageTitle.pack(side='top', fill='x')
self.panelBlock.pack(side='top', fill='x', pady=5)
self.panelLabel.pack(side='left', fill='x', padx=50)
self.panelSetting.pack(side='right', fill='x')
#actions
self.refreshSelf() ##i know that this doesn't work, but i do not understand why
oPanelSettings.cutLength.trace('r', self.refreshSelf) ##i really feel like this should work though
def refreshSelf(self):
#pages to objects
oPanelSettings = pPanelSettings(self)
#action
self.after(1000, self.refreshSelf)
self.panelSetting.config(text=oPanelSettings.cutLength.get())
class pMainView(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
#pages to objects
oPanelSettings = pPanelSettings(self)
oViewSettings = pViewSettings(self)
#create menu and view windows
buttonFrame = tk.Frame(self)
container = tk.Frame(self)
buttonFrame.pack(side='left', fill='both', expand=False) #menu column
container.pack(side='right', fill='both', expand=True) #main view window
#place page objects into main view window
oPanelSettings.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
oViewSettings.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
#create menu buttons
bPanelSettings = tk.Button(buttonFrame, text='Panel Settings', command=oPanelSettings.lift)
bViewSettings = tk.Button(buttonFrame, text='View Settings', command=oViewSettings.lift)
#menu layout
bPanelSettings.pack(side='top', fill='both', expand=True)
bViewSettings.pack(side='top', fill='both', expand=True)
#startup display page
oViewSettings.show()
if __name__ == '__main__':
root = tk.Tk()
oMainView = pMainView(root)
oMainView.pack(side='top', fill='both', expand=True)
root.wm_geometry('1024x600')
root.mainloop()
非常感谢任何和所有帮助。有人打电话给布莱恩奥克利来救我!:) :)
编辑#1:缩短代码以实现最小复制。
解决方案
使用 StringVar 更新标签的正确方法如下:
这是一个最小的示例,您可以单击“添加”按钮来增加值并在“红色”和“青色”之间更改标签颜色。
import tkinter as tk
root = tk.Tk()
value = 0
labelVar = tk.StringVar()
labelVar.set(f"Current value: {value}")
def add_text():
global value
value += 1
labelVar.set(f"Current value: {value}")
if value % 2:
newcolor = 'red'
else:
newcolor = 'cyan'
label.config(bg=newcolor)
label = tk.Label(root, textvariable=labelVar, bg='cyan')
label.pack()
tk.Button(root, text='Add', command=add_text).pack()
root.mainloop()
推荐阅读
- tensorflow - 由于 Keras,模块“tensorflow”没有属性“占位符”
- ios - 退出 UIViewController 时禁用“保存密码”操作表?
- c# - 不记名令牌的 NSwag CSharpClientGenerator 自定义请求标头
- generics - 如何使用闭包抽象数据和操作
- nativescript - {NS}-Vue 中的 RadAutocomplete 问题
- python - 使用 python + selenium+ 登录网站
- c++ - 链表概念
- c# - asp.net core 中的 URL 问题
- vue.js - Vue Cypress 和 Gitlab CI/CD
- c++ - Arduino - 在一定时间内运行 if 条件