Python 3.4 / Tkinter跨类变量访问

时间:2014-11-30 22:42:31

标签: python tkinter

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

错误:

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

对于我的生活无法理解这一点我知道它会变得简单,我会自己踢。

1 个答案:

答案 0 :(得分:2)

您正在使用未绑定方法,您应该使用self创建绑定方法。替换:

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

使用:

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

请注意,这些项目都不会存储在您的实例或类中;没有设置layOut.velayOut.ielayOut.re,因为您将veiere作为本地人。最好在实例上设置(在self上设置),然后在self中再次查看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)