Tkinter类模块引用异常

2024-10-02 22:37:52 发布

您现在位置:Python中文网/ 问答频道 /正文

调用类(Eventsim)中的模块(updateUI)时遇到一些问题。你知道吗

line Sim=EventSim()抛出异常,因为它缺少参数(父级)。我不知道如何修复/引用父对象。你知道吗

这是我第一次尝试使用Tkinter,我的python知识也相当有限(目前)。你知道吗

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

Tags: textselfipbuttonleftfillframeside
2条回答

parent应该是root。因此,替换:

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


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

使用:

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

打开所需的窗口。updateUI方法需要工作来填充输入字段,但是您可以删除它的parent参数,因为您已经有了parent实例变量。你知道吗

移除Sim = EventSim()并将Sim.updateUI('1','1','1')移动到main

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

main()

相关问题 更多 >