简体   繁体   English

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

[英]Inserting a Variable into Tkinter Listbox from Another Class

I am trying to insert a variable named 'Variable A' from 'Class_A' class into a Listbox which is at another class named 'App(tk)'. 我试图将来自“ Class_A”类的名为“变量A”的变量插入到名为“ App(tk)”的另一个类的列表框中。 Can anyone help me? 谁能帮我?

The result should look like this: 结果应如下所示: 理想结果 .

However when using the code I have and after hitting the 'Run' button, it opens up a new window (which is identical as the main window) instead of inserting 'Variable A' into 'positive' Listbox at the existing window. 但是,当使用我拥有的代码并单击“运行”按钮后,它将打开一个新窗口(与主窗口相同),而不是在现有窗口的“正”列表框中插入“变量A”。 实际结果 and I also receive an error: AttributeError: '_tkinter.tkapp' object has no attribute 'myListbox_01' 并且我还收到一个错误:AttributeError:'_tkinter.tkapp'对象没有属性'myListbox_01'

Here is the code: 这是代码:

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

You're getting a new window because your class_A instance creates a new App instance in turnOn . 您将获得一个新窗口,因为您的class_A实例在turnOn创建了一个新的App实例。 Probably you want to pass the existing App to the class at some point (either to the constuctor, or to turnOn itself). 可能您希望在某个时候将现有的App传递给类(传递给构造turnOn ,或者传递turnOn自身)。

Here's a quick and dirty fix. 这是一个快速而肮脏的修复程序。 A better version would probably keep the app value in a instance variable in class_A , rather than passing it to turnOn only (you might also want your App to keep a single instance of class_A , rather than creating a new one each time the button is pressed): 更好的版本可能会将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()

        # ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM