使用Tkinter开发GUI时金字塔体积无法显示问题求助
Tkinter金字塔体积计算结果无法显示问题排查与解决
在使用Tkinter开发GUI时,计算金字塔体积后无法在界面显示结果。添加调试打印后,发现打印self.volume输出为"PY_VAR0",调用set()方法赋值计算结果后仍无任何显示。相关代码如下:
def pyramid_button(self): # create the pyramid window self.pyramid_button_window = tkinter.Tk() self.pyramid_button_window.geometry('300x300') self.pyramid_button_window.title('Pyramid Volume') # create frames for secondary windows self.length_frame = tkinter.Frame(self.pyramid_button_window) self.width_frame = tkinter.Frame(self.pyramid_button_window) self.height_frame = tkinter.Frame(self.pyramid_button_window) self.volume_frame = tkinter.Frame(self.pyramid_button_window) self.button_frame = tkinter.Frame(self.pyramid_button_window) # create and pack widgets for pyramid length self.length_label = tkinter.Label(self.length_frame, text='Enter the base length of the pyramid:') self.length_entry = tkinter.Entry(self.length_frame, width=10) self.length_label.pack(side='left') self.length_entry.pack(side='left') # create and pack widgets for pyramid width self.width_label = tkinter.Label(self.width_frame, text='Enter the base width of the pyramid:') self.width_entry = tkinter.Entry(self.width_frame, width=10) self.width_label.pack(side='left') self.width_entry.pack(side='left') # create and pack widgets for pyramid height self.height_label = tkinter.Label(self.height_frame, text='Enter the base width of the pyramid:') self.height_entry = tkinter.Entry(self.height_frame, width=10) self.height_label.pack(side='left') self.height_entry.pack(side='left') # create and pack widgets for calculated pyramid volume self.volume_label = tkinter.Label(self.volume_frame, text='Volume: ') self.volume = tkinter.StringVar() self.calculated_volume_label = tkinter.Label(self.volume_frame, textvariable=self.volume) self.volume_label.pack(side='left') self.calculated_volume_label.pack(side='left') # create and pack button widgets self.p_calc_button = tkinter.Button(self.button_frame, text='Volume', command=self.calc_p_volume) self.p_close_button = tkinter.Button(self.button_frame, text='Close', command=self.pyramid_button_window.destroy) self.p_calc_button.pack(side='left') self.p_close_button.pack(side='left') # Pack the frames self.length_frame.pack() self.width_frame.pack() self.height_frame.pack() self.volume_frame.pack() self.button_frame.pack() def calc_p_volume(self): # get length, width, height and store them # in variables self.length = float(self.length_entry.get()) self.width = float(self.width_entry.get()) self.height = float(self.height_entry.get()) print(self.height, self.width, self.length) # calculate the volume self.p_volume = ((self.length * self.width * self.height)/3) print(self.p_volume) # update the volume label with calculated self.p_volume self.volume.set(self.p_volume) print(self.volume)
问题原因
核心问题是多Tk实例的变量绑定冲突:
- Tkinter中每个
StringVar默认绑定到程序启动时创建的根Tk实例,你在子窗口中新建了第二个Tk()实例,导致self.volume绑定的是根实例而非子窗口实例。 - 调用
self.volume.set()时,更新的是根实例绑定的变量,子窗口的标签无法感知到这个变化,所以界面无显示。 - 打印
self.volume显示"PY_VAR0"是正常现象,这是StringVar对象的默认内部标识,并非错误。
解决方案
方案1:用Toplevel替代Tk创建子窗口
Toplevel是依附于主窗口的子窗口,会自动共享根Tk实例的变量上下文,是创建子窗口的标准方式:
def pyramid_button(self): # 将Tk()替换为Toplevel() self.pyramid_button_window = tkinter.Toplevel() self.pyramid_button_window.geometry('300x300') self.pyramid_button_window.title('Pyramid Volume') # 后续代码保持不变
方案2:为StringVar指定正确的master
如果必须使用独立的Tk实例,创建StringVar时明确指定其所属的窗口实例:
# 在创建volume变量时指定master为子窗口 self.volume = tkinter.StringVar(master=self.pyramid_button_window)
额外提示
代码中存在一个小错误:高度输入框的标签文本写错了,当前是Enter the base width of the pyramid:,应该改为Enter the height of the pyramid:,避免用户输入混淆。
内容的提问来源于stack exchange,提问作者SheriffWoody95
相关产品推荐
相关产品推荐

