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!

All of the widget.get() statements are called when the widgets are created, i.e. before anything is entered. See the example for the Entry using a button to get the results, the other widgets are the same at effbot http://effbot.org/tkinterbook/entry.htm So start with one widget and test it until it does what you want, and then add more.

I have tried that I'll post an edited 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_Right = 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,measurements, measurements1, Text_Length_Left, Text_Length_Right):
        try:
            v = float(self.Text_Length_Left.get())
        except ValueError:
            self.Text_Length_Left.set('')
            self.Text_Length_Right.set('')
            return
        m = v * self.as_meter [self.measurements.get()]
        r = m/self.as_meter[self.measurements1.get()]
        self.Text_Length_Right.set("{:.3g}".format(r))

class GUI:
    def __init__(self,root):
        global a,b,c,d
        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)

        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', 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)

        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)

        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()

        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)

        a = self.measurements
        b = self.measurements1
        c = self.Text_Length_Left
        d = self.Text_Length_Right
        Logic(a,b,c,d)




root = Tk()
app = GUI(root)
root.title('Metric Calculator')
root.resizable(False,False)
root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.