Hi there!
I am having trouble figuring out what is wrong with my code also I'm kinda new to Python. I'm trying to get variables to one class to another class then return it back. Here is my code:
from tkinter import *
from tkinter import ttk
class Logic:
def __init__(self,measurements,measurements1,Text_Length_Left,Text_Length_Right):
self.measurements = measurements
self.measurements1 = measurements1
self.Text_Length_Left = Text_Length_Left
self.Text_Length_Leftset = Text_Length_Leftset
self.Text_Length_Right = Text_Length_Right
self.Text_Length_Rightset = Text_Length_Rightset
#=========================Length Calculations=======================#
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):
try:
v = float(self.Text_Length_Left)
except ValueError:
self.Text_Length_Leftset('')
self.Text_Length_Rightset('')
return
m = v * self.as_meter [self.measurements.get()]
r = m/self.as_meter[self.measurements1.get()]
self.Text_Length_Rightset("{:.3g}".format(r))
#============================GUI Class=============================#
class GUI:
def __init__(self,a,b,c,d,e,f):
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.help_frame = ttk.Frame(self.notebook)
#=======================Length Frame===============================#
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)
a = self.measurements.get()
self.measurements.trace('w', Logic(self.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)
b = self.measurements1.get()
self.measurements1.trace('w', Logic(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 = 'Convertion:').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)
c = self.Text_Length_Left.get()
d = self.Text_Length_Left.set()
self.Text_Length_Left.trace('w', Logic(self.update_length))
self.Entry_Length_Left.grid(row = 3, column = 0, padx = 5, pady = 5)
self.Text_Length_Right = StringVar()
e = self.Text_Length_Right.get()
f = self.Text_Length_Right.set()
self.Entry_Length_Right = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Right,width = 13, state='readonly')
self.Entry_Length_Right.grid(row = 3, column = 2, padx = 5, pady = 5)
Logic(a,b,c,d,e,f)
def main():
root = Tk()
GUI(root,a,b,c,d,e,f)
root.title('Metric Calculator')
root.resizable(False,False)
root.mainloop()
main()
Thanks for any help!