我正在尝试从名为StringVar
的类调用变量或GUI
而不调用GUI
作为类Conversion
的对象。以下是我的代码示例:
class GUI:
def __init__(self,root):
c = Conversion(measurements,measurements1,Text_Legnth_Left,Text_Length_Right)
measurements = self.measurements
measurements1 = self.measurements1
Text_Length_Left = self.Text_Legnth_Left
Text_Length_Right = self.Text_Length_Right
self.notebook = ttk.Notebook(root)
self.notebook.pack()
self.length_frame = ttk.Frame(self.notebook)
self.weight_frame = ttk.Frame(self.notebook)
self.temperature_frame = ttk.Frame(self.notebook)
self.notebook.add(self.length_frame, text = 'Length')
#Combobox
self.measurements = StringVar()
self.Combobox_Length_Left = ttk.Combobox(self.length_frame, textvariable = self.measurements, values = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state=['readonly'])
self.Combobox_Length_Left.current(5)
self.measurements.trace('w', c.update_length())
self.Combobox_Length_Left.grid(row = 2, column = 0, padx = 5, pady = 5, sticky = E)
self.measurements1 = StringVar()
self.Combobox_Length_Right = ttk.Combobox(self.length_frame, textvariable = self.measurements1, value = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state = ['readonly'])
self.Combobox_Length_Right.current(5)
self.measurements1.trace('w', c.self.update_length())
self.Combobox_Length_Right.grid(row = 2, column = 2, padx = 5, pady = 5, sticky = E)
#Labels
self.Conversion = ttk.Label(self.length_frame, text = 'Conversion:').grid(row = 1, column = 0, padx = 5, pady = 5, sticky = W)
self.Label_Blank = ttk.Label(self.length_frame, text = '').grid(row = 1, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To2 = ttk.Label(self.length_frame, text = 'to').grid(row = 2, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To = ttk.Label(self.length_frame, text = 'to').grid(row = 3, column = 1, padx = 5, pady = 5, sticky = E)
#Entry Boxes
self.Text_Length_Left = StringVar()
self.Entry_Length_Left = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Left,width = 13)
self.Text_Length_Left.trace('w', self.update_length)
self.Entry_Length_Left.grid(row = 3, column = 0, padx = 5, pady = 5)
self.Text_Length_Right = StringVar()
self.Entry_Length_Right = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Right,width = 13, state='readonly')
这是我的另一堂课:
class Conversion:
def __init__(self,measurements,measurements1,Text_Legnth_Left,Text_Length_Right):
g = GUI(measurements,measurements1,Text_Legnth_Left,Text_Length_Right)
as_meter = dict(mm=0.001, cm=0.01, inch=0.0254,ft=0.3048, yd=0.9144, m=1.0,km=1000.0, mi=1609.344,)
def update_length(self, *args,g):
try:
v = int(g.self.Text_Length_Left.get())
except ValueError:
g.self.Text_Length_Left.set('')
g.self.Text_Length_Right.set('')
return
m = v * self.as_meter [g.self.measurements.get()]
r = m/self.as_meter[g.self.measurements1.get()]
g.self.Text_Length_Right.set("{:.5g}".format(r))
我调用measurements
等变量的尝试是c=Conversion(measurements)
我知道做class GUI(Conversion)
会有效并使其成为对象,但由于特定原因我不能这样做。
感谢任何帮助,真的很感激!