Robert Altman Robert Altman - 9 months ago 37
Python Question

Tkinter class module reference exception

I'm having some trouble calling a module(updateUI) within a class (Eventsim).

The line Sim = EventSim() throws an exception because it's missing an argument (parent). I can't figure out how to fix this / reference the parent object.

This is my first attempt wit Tkinter and my python knowledge is also rather limited (for now).

from Tkinter import *


class EventSim(Frame):

def __init__(self, parent):
Frame.__init__(self, parent)

self.parent = parent

def updateUI(self,IP_Address,Port_Number,Events_Directory):

self.parent.title('ECP Event Simulator')
self.parent.resizable(0, 0)
self.pack(fill=BOTH, expand=True)

frame1 = Frame(self)
frame1.pack(fill=X)
frame2 = Frame(self)
frame2.pack(fill=X)
frame3 = Frame(self)
frame3.pack(fill=X)
frame4 = Frame(self)
frame4.pack(fill=X)
frame5 = Frame(self)
frame5.pack(fill=X)
frame6 = Frame(self)
frame6.pack(fill=X,pady=(10,30))
frame7 = Frame(self)
frame7.pack(fill=X)
frame8 = Frame(self)
frame8.pack(fill=X,pady=(10,0))


Main_Label = Label(frame1,text='ECP EventSim')
Main_Label.pack(side=LEFT,padx=100)

IP_Label = Label(frame2,text='IP Address:')
IP_Label.pack(side=LEFT,padx=10)
Port_Label = Label(frame2,text='Port:')
Port_Label.pack(side=RIGHT,padx=70)

IP_Text = Entry(frame3)
IP_Text.pack(fill=X,side=LEFT,padx=10)
IP_Text = Entry(frame3)
IP_Text.pack(fill=X,side=RIGHT,padx=10)

Dir_Label = Label(frame4,text='Events Directory:')
Dir_Label.pack(side=LEFT,padx=10)
Dir_Text = Entry(frame5)
Dir_Text.pack(fill=X,side=LEFT,padx=10,expand=True)

Save_Button = Button(frame6,text='Save Config')
Save_Button.pack(fill=X,side=LEFT,padx=10,expand=True)
Con_Button = Button(frame7,text='Connect')
Con_Button.pack(fill=X,side=LEFT,padx=10,expand=True)
Send_Button = Button(frame8,text='Start Sending Events')
Send_Button.pack(fill=X,side=LEFT,padx=10,expand=True)

def main():
root = Tk()
root.geometry("300x300+750+300")
app = EventSim(root)
root.mainloop()


Sim = EventSim()
Sim.updateUI('1','1','1')
main()

Answer

The parent should be root. So, replacing:

def main():
    root = Tk()
    root.geometry("300x300+750+300")
    app = EventSim(root)
    root.mainloop()


Sim = EventSim()
Sim.updateUI('1','1','1')
main()

with:

root = Tk()
root.geometry("300x300+750+300")
Sim = EventSim(root)
Sim.updateUI('1','1','1')
root.mainloop()

brings up the desired window. The updateUI method requires work to populate the entry fields but you can remove its parent parameter since you already have the parent instance variable.