Having problems using Tkinter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • viv1tyagi@gmail.com

    Having problems using Tkinter

    Hi everyone ! ! !

    I'm just a month old in the world of Python and trying to develop an
    application using Tkinter in which a new window pops out on a
    particular button press.The code for this new window is in
    "AddKlas.py " file.
    The problem is all the content of the new window is overwritten on
    the previous window.
    Below is the code of the two files

    PLEASE help me out ! ! ! !

    #1st file
    #Klass.py
    from Tkinter import *
    import AddKlass
    class Klass(Frame):
    def __init__(self):
    self.labels()
    self.buttons()
    self.menubutton s()
    self.scrollandc anvas()


    def labels(self):
    label1=Label(re lief='groove',t ext=" Class
    Id",width=20,an chor=W)
    label1.grid(row =0,column=0)
    label2=Label(re lief='groove',t ext=" Class
    Name",width=20, anchor=W)
    label2.grid(row =0,column=1)
    label3=Label(re lief='groove',t ext=" Class Room
    No.",width=20,a nchor=W)
    label3.grid(row =0,column=2)
    label4=Label(re lief='groove',t ext=" Category
    Id",width=80,an chor=W)
    label4.grid(row =0,column=3)

    def buttons(self):
    button1=Button( text="Add",widt h=10,command=se lf.CallAdd)
    button1.grid(ro w=2,column=5)
    button2=Button( text="View",wid th=10,state=DIS ABLED)
    button2.grid(ro w=3,column=5)
    button3=Button( text="Next",wid th=10,state=DIS ABLED)
    button3.grid(ro w=4,column=5)

    def menubuttons(sel f):
    menubutton=Menu button(text="Op tions",width=10 ,relief="raised ")
    menubutton.grid (row=5,column=5 )
    menubutton.menu =Menu(menubutto n)
    menubutton["menu"]=menubutton.men u
    menubutton.menu .add_command(la bel="Modify")
    menubutton.menu .add_command(la bel="Delete")

    def scrollandcanvas (self):
    yscroll=Scrollb ar()
    yscroll.grid(ro w=1,column=4,ro wspan=7,sticky= N+S)
    canva=Canvas(bg ="#fff",yscroll command=yscroll .set)
    canva.grid(row= 1,column=0,rows pan=7,columnspa n=4,sticky=N+E+ S
    +W)

    def CallAdd(self):
    AddKlass.root=T k()
    AddKlass.AddKla s()
    AddKlass.root.f ocus_force()
    AddKlass.root.g eometry("275x25 0")
    AddKlass.root.t itle("Add Class Information")
    AddKlass.root.r esizable(width= False,height=Fa lse)
    AddKlass.root.m ainloop()

    root=Tk()
    app = Klass()
    root.geometry(" 960x300")
    root.title("Cla ss Information")
    root.resizable( width=False,hei ght=False)
    root.mainloop()




    #2nd file
    #AddKlass.py
    from Tkinter import *

    class AddKlas(Frame):
    #root=None
    def __init__(self):
    self.labels()
    self.buttons()
    self.entry()

    def labels(self):
    label1=Label(re lief='flat',tex t=" Class
    Id :",height=3,wid th=20)
    label1.grid()
    label2=Label(re lief='flat',tex t=" Class
    Name :",height=3,wid th=20)
    label2.grid()
    label3=Label(re lief='flat',tex t=" Class Room
    No. :",height=3,wid th=20)
    label3.grid()
    label4=Label(re lief='flat',tex t=" Category
    Id :",height=3,wid th=20)
    label4.grid()

    def buttons(self):
    button1=Button( text="Add",widt h=10,state=DISA BLED)
    button1.grid(ro w=5,column=0)
    button2=Button( text="Cancel",w idth=10,command =self.quit)
    button2.grid(ro w=5,column=1)

    def entry(self):
    entry1=Entry() #by default width =20
    entry1.grid(row =0,column=1)
    entry2=Entry()
    entry2.grid(row =1,column=1)
    entry3=Entry()
    entry3.grid(row =2,column=1)
    entry4=Entry()
    entry4.grid(row =3,column=1)

    #root=Tk()
    #app = AddKlas()
    #root.geometry( "275x250")
    #root.title("Ad d Class Information")
    #root.resizable (width=False,he ight=False)
    #root.mainloop( )


  • Marc 'BlackJack' Rintsch

    #2
    Re: Having problems using Tkinter

    On Tue, 01 Jul 2008 03:13:42 -0700, viv1tyagi wrote:
    Hi everyone ! ! !
    >
    I'm just a month old in the world of Python and trying to develop an
    application using Tkinter in which a new window pops out on a
    particular button press.The code for this new window is in
    "AddKlas.py " file.
    The problem is all the content of the new window is overwritten on
    the previous window.
    Below is the code of the two files
    There can be only one `Tkinter.Tk` instance per running program. Other
    top level windows have to be `Tkinter.Toplev el` instances.

    And you should reconsider your usage of classes. You are just abusing
    them as containers for functions without using `self` really. And you
    should pass the instance of the parent widget as first argument to
    constructors of widgets. Otherwise the very first, i.e. the `Tk` instance,
    is assumed, which is not true for the widgets that should appear in the
    `Toplevel` instance.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    Working...