rvncpn
rvncpn

Reputation: 85

Python 3.4/Tkinter cross class variable accessing

__author__ = 'rvncpn'

import tkinter
from tkinter import ttk


class layOut(tkinter.Tk):
    volts = 0
    amps = 0
    ohms = 0

    def updizzle(self):
        layOut.volts = layOut.ve.get()
        layOut.amps = layOut.ie.get()
        layOut.ohms = layOut.re.get()

    def __init__(self, *args, **kwargs):
        tkinter.Tk.__init__(self, *args, **kwargs)
        container = ttk.Frame(self)
        container.grid(columns=3, rows=3, sticky='nsew')
        tkinter.Tk.title(self, 'Ohms Law v2')

        v = ttk.Label(container, text='Volts')
        v.grid(column=0, row=0)

        i = ttk.Label(container, text='Amperes')
        i.grid(column=1, row=0)

        r = ttk.Label(container, text='Ohms')
        r.grid(column=2, row=0)

        ve = ttk.Entry(container)
        ve.grid(column=0, row=1)

        ie = ttk.Entry(container)
        ie.grid(column=1, row=1)

        re = ttk.Entry(container)
        re.grid(column=2, row=1)

        button = ttk.Button(container, text='Button', command=layOut.updizzle)
        button.grid(column=2, row=2,pady=6)

        answer = ttk.Label(container, text='32095782hirjfkebfesfme')
        answer.grid(column=0, row=2, columnspan=2)


class ohms(layOut):
    def __init__(self):
        self.v = layOut.volts
        self.i = layOut.amps
        self.r = layOut.ohms
        self.w = []

    def sorter(self):
        if self.v > 0:
            self.w.append('volts')
        if self.i > 0:
            self.w.append('amps')
        if self.r > 0:
            self.w.append('ohms')

    def listCheck(self):
        if 'volts' in self.w and 'amps' in self.w and 'ohms' in self.w:
            print('you must leave one of the perimeter empty')
        elif 'volts' in self.w and 'amps' in self.w:
            print('resistance =', self.v / self.i)
        elif 'volts' in self.w and 'ohms' in self.w:
            print('amps =', self.i / self.i)
        elif 'amps' in self.w and 'ohms' in self.w:
            print('volts=', self.i * self.r)
        else:
            pass


app = layOut()
app.mainloop()

Error:

TypeError: updizzle() missing 1 required positional argument: 'self'

Can not figure this out for the life of me I know its going to be something simple and I'm going to kick myself.

Upvotes: 0

Views: 114

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121276

You are using the unbound method, where you should use self to create a bound method. Replace:

button = ttk.Button(container, text='Button', command=layOut.updizzle)

with:

button = ttk.Button(container, text='Button', command=self.updizzle)

Note that none of these items is stored on your instance or class; there is no layOut.ve, layOut.ie or layOut.re being set, because you left ve, ie and re as locals. Best set those on the instance (so on self), then look them up on self again in updizzle:

class layOut(tkinter.Tk):
    volts = 0
    amps = 0
    ohms = 0

    def updizzle(self):
        self.volts = self.ve.get()
        self.amps = self.ie.get()
        self.ohms = self.re.get()

    def __init__(self, *args, **kwargs):
        tkinter.Tk.__init__(self, *args, **kwargs)
        container = ttk.Frame(self)
        container.grid(columns=3, rows=3, sticky='nsew')
        tkinter.Tk.title(self, 'Ohms Law v2')

        v = ttk.Label(container, text='Volts')
        v.grid(column=0, row=0)

        i = ttk.Label(container, text='Amperes')
        i.grid(column=1, row=0)

        r = ttk.Label(container, text='Ohms')
        r.grid(column=2, row=0)

        self.ve = ttk.Entry(container)
        self.ve.grid(column=0, row=1)

        self.ie = ttk.Entry(container)
        self.ie.grid(column=1, row=1)

        self.re = ttk.Entry(container)
        self.re.grid(column=2, row=1)

        button = ttk.Button(container, text='Button', command=layOut.updizzle)
        button.grid(column=2, row=2,pady=6)

        answer = ttk.Label(container, text='32095782hirjfkebfesfme')
        answer.grid(column=0, row=2, columnspan=2)

Upvotes: 2

Related Questions