Reputation: 1341
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)'. 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. and I also receive an error: AttributeError: '_tkinter.tkapp' object has no attribute '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()
Upvotes: 0
Views: 757
Reputation: 104712
You're getting a new window because your class_A
instance creates a new App
instance in turnOn
. Probably you want to pass the existing App
to the class at some point (either to the constuctor, or to turnOn
itself).
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):
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()
# ...
Upvotes: 1