问题描述
目标:显示具有给定标题和几何形状的 CalibrationWindow,并填充有标签、输入字段、3 个缩放小部件和 2 个按钮,同时通过 ssh -Y 连接。
预期结果:参见上面的目标。
实际结果:未出现校准设置窗口。终端窗口显示“AttributeError: 'CalibrationWindow' 对象没有属性 'settings'。”
我的代码有两个独立的 SettingsWindow 子类,它们都遇到了这个问题。我在这里只包括了其中之一。不知何故,子类无法找到实例属性“设置”。我还尝试使用 @property 进行 getter 和 setter 设置,但这并不能解决错误。 我通过 ssh 运行它,当我只调用 Window() 时,它会按预期打开空窗口。当我调用 SettingsWindow()
import pickle
from tkinter import *
import tkinter.font as tkFont
import sys
import os
settings_filepath = "myapp.conf"
calibrationWindowTitle = "Calibration Settings"
calibrationWindowGeometry = "220x180"
class Settings:
def __init__(self):
self.dirty = False
self.Load()
def LoadDefaults(self):
self.dirty = True
self.data = SettingsData("Default Settings")
def Load(self):
if os.path.exists(settings_filepath):
try:
with open(settings_filepath,'rb') as f:
self.data = pickle.load(f)
except:
print('Couldn\'t open settings file. Using defaults instead.')
self.LoadDefaults()
else:
self.LoadDefaults()
def Update(self,newData):
self.data = newData
self.dirty = True
def Save(self):
if self.dirty:
try:
with open(settings_filepath,'wb') as f:
pickle.dump(self.data,f,pickle.HIGHEST_PROTOCOL)
except:
print('Couldn\'t save settings. Try again or accept defaults.')
class Window(object):
def __init__(self,title,geometry):
self.window = Tk()
self.font = tkFont.Font(root=self.window,family= 'Helvetica',size = 12,weight = 'bold')
self.window.title = title # No visible effect.
self.window.geometry = geometry # No visible effect.
self.AddControls()
self.Show()
def AddControls(self):
# Child classes must override this method.
pass
def Show(self):
# Child clsss must override this method to insert ".pack()" calls for each control before calling super().Show().
self.window.mainloop()
def Save(self):
# Child classes must override this method if they have anything that needs saving.
self.Close()
def Close(self):
# Child classes must override this method if they have anything that needs deallocation.
self.window.destroy()
class SettingsWindow(Window):
def __init__(self,geometry):
super().__init__(title,geometry)
self.settings = Settings()
def Save(self):
self.settings.Save()
class CalibrationWindow(SettingsWindow):
def __init__(self,geometry,unitIndex):
super().__init__(title,geometry)
self.unitIndex = unitIndex
def AddControls(self):
calib = self.settings.calibrations[self.unitIndex]
### WIDGETS ###
self.myLabel = Label(self.window,text="Name:")
self.myLabel.grid(row=0,column=0)
self.myField = Entry(self.window,width=50,variable=self.settings.calibrations[self.unitIndex].unitName,command=self.UnitNameChanged())
self.myField.grid(row=0,column=2)
self.minSlider = Scale(self.window,label='Minimum',font=self.font,bg='bisque2',height=1,length=200,cursor='Hand',from_=0,to=100,command=self.SettingsChanged())
self.minSlider.grid(row=1,column=1)
self.centerSlider = Scale(self.window,label='Center',command=self.SettingsChanged())
self.centerSlider.grid(row=2,column=1)
self.maxSlider = Scale(self.window,label='Maximum',command=self.SettingsChanged())
self.maxSlider.grid(row=3,column=1)
self.cancelButton = Button (self.window,text='Cancel',bg='red',width=6,command=self.Cancel())
self.cancelButton.grid(row=4,column=2)
self.exitButton = Button (self.window,text='Save',bg='green',command=self.Close())
self.exitButton.grid(row=4,column=3)
self.myLabel.pack()
self.myField.pack()
self.minSlider.pack(anchor=center)
self.centerSlider.pack(anchor=center)
self.maxSlider.pack(anchor=center)
self.cancelButton.pack()
self.saveButton.pack()
super().Show()
def UnitNameChanged(self):
self.dirty = true
def SettingsChanged(self):
self.dirty = true
#Test calibrations window:
CalibrationsWindow(calibrationWindowTitle,calibrationWindowGeometry)
我在这里错过了什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)