将变量插入另一个类的Tkinter列表框中

时间:2016-07-10 22:35:15

标签: python python-3.x tkinter

我正在尝试插入名为'变量A'的变量。来自' Class_A'将类分类到另一个名为' App(tk)'的类的列表框中。任何人都可以帮助我吗?

结果应如下所示:Ideal Result

然而,当我使用我的代码时,以及在运行'运行'按钮,它打开一个新窗口(与主窗口相同),而不是插入变量A'进入'积极'现有窗口中的列表框。 Actual Result我也收到错误:AttributeError:' _tkinter.tkapp'对象没有属性' myListbox_01'

以下是代码:

from tkinter import *
import tkinter.messagebox as tkMessageBox
import tkinter.filedialog as tkFileDialog


class class_A(object):
    def __init__(self, isON = False):
        self.isOn = False
    def turnOn(self):
        self.isOn = True
        tkMessageBox.showinfo(title = 'On', message = 'It is on')
        x = 'favorable'
        if x == 'favorable':
            temp_App = App()
            temp_App.myListbox_01.insert(END, 'Variable A')
        else:
            temp_App.myListbox_02.insert(END, 'Variable A')

    def turnOff(self):
        self.isOn = False
        tkMessageBox.showinfo(title = 'Off', message = 'It is off')


class App(Tk):
    def __init__(self):
        Tk.__init__(self)

        def toggle():

            if button.config('text')[-1] == 'Run':
                A = class_A()
                A.turnOn()
                button.config(text='Stop')
            else:
                button.config(text='Run')

                A = class_A()
                A.turnOff()


        Frame_0 = Frame(self, bg = 'Black', borderwidth = 2, relief = GROOVE)
        Frame_0.pack(side = TOP, padx = 10, pady = 2.5)

        # Positive
        Frame_title01 = Frame(Frame_0, bg="white", height = 10, width = 300, borderwidth = 2, relief=GROOVE)
        Frame_title01.grid(row = 0, column = 0, padx=5, pady=5)
        Label(Frame_title01, text="positive").pack(padx=2, pady=2)

        Frame_01 = Frame(Frame_0, bg="white", height = 200, width = 300, borderwidth = 2, relief=GROOVE)
        Frame_01.grid(row = 1, column = 0, padx=5, pady=5)

        myListbox_01 = Listbox(Frame_01, bg = 'white', width = 15, height = 10, font = ('times', 14), borderwidth=0)
        myListbox_01.grid(row = 0, column = 0, padx = 80, pady = 5)

        # Negative
        Frame_title02 = Frame(Frame_0, bg="white", height = 10, width = 300, borderwidth = 2, relief=GROOVE)
        Frame_title02.grid(row = 0, column = 1, padx=2, pady=2)
        Label(Frame_title02, text="Negative").pack(padx=2, pady=2)

        Frame_02 = Frame(Frame_0, bg="white", height = 200, width = 300, borderwidth = 2, relief=GROOVE)
        Frame_02.grid(row = 1, column = 1, padx=5, pady=5)

        myListbox_02 = Listbox(Frame_02, bg = 'white', width = 15, height = 10, font = ('times', 14), borderwidth=0)
        myListbox_02.grid(row = 0, column = 0, padx = 80, pady = 5)

        # Button        
        Frame_1 = Frame(self, bg = 'white', borderwidth = 2, relief = FLAT)
        Frame_1.pack(side = TOP)

        button = Button(Frame_1, text = 'Run', command = toggle)
        button.pack(pady = 10)



if __name__ == "__main__":
    app = App()
    app.geometry("800x300+51+51")
    app.title("GUI")
    app.mainloop()

1 个答案:

答案 0 :(得分:1)

您正在获取一个新窗口,因为您的class_A实例在App中创建了一个新的turnOn实例。您可能希望在某个时刻将现有的App传递给类(要么是构造函数,要么传递给turnOn本身。)

这是一个快速而肮脏的修复方法。更好的版本可能会将app值保留在class_A中的实例变量中,而不是仅将其传递给turnOn(您可能还希望App保留一个class_A的实例,而不是每次按下按钮时都创建一个新实例:

class class_A(object):
    def __init__(self, isON = False):
        self.isOn = False
    def turnOn(self, app):              # new app arg!!!
        self.isOn = True
        tkMessageBox.showinfo(title = 'On', message = 'It is on')
        x = 'favorable'
        if x == 'favorable':
            app.myListbox_01.insert(END, 'Variable A')   # use new arg here
        else:
            app.myListbox_02.insert(END, 'Variable A')   # and here

    def turnOff(self):
        self.isOn = False
        tkMessageBox.showinfo(title = 'Off', message = 'It is off')

class App(Tk):
    def __init__(self):
        Tk.__init__(self)

        def toggle():

            if button.config('text')[-1] == 'Run':
                A = class_A()
                A.turnOn(self)                  # pass self as app arg!
                button.config(text='Stop')
            else:
                button.config(text='Run')

                A = class_A()
                A.turnOff()

        # ...