Strange .grid sectioning? Tkinter?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 360monkey
    New Member
    • Feb 2010
    • 17

    Strange .grid sectioning? Tkinter?

    Hey Bytes Community!

    I am currently using Tkinter to create a GUI window, and I ran into a problem. Here is my code:

    Code:
    from Tkinter import *
    import random
    import gameinclude
    gi = gameinclude
    
    class App:
    
    	def __init__(self, master):
    	##########
    	#Menu Bar#
    	##########
    		frame = Frame(master)
    		frame.grid()
    		menu = Menu(root)
    		root.config(menu=menu)
    		filemenu = Menu(menu)
    		menu.add_cascade(label='File', menu=filemenu)
    		filemenu.add_command(label='Quit', command=frame.quit)
    		infomenu = Menu(menu)
    		menu.add_cascade(label='About...', menu=infomenu)
    		infomenu.add_command(label='About BattlerIII', command=None)
    	############
    	#Name Entry#
    	############
    		self.pone = Label(frame, text='Player One').grid(row=0, column=0)
    		self.epone = Entry(frame, textvariable = StringVar)
    		self.epone.grid(row=0, column=1)
    		self.ptwo = Label(frame, text='Player Two').grid(row=0, column=4)
    		self.eptwo = Entry(frame, textvariable = StringVar)
    		self.eptwo.grid(row=0, column=5)
    		self.activebtn = Button(frame, text='GO!', command = self.interface)
    		self.activebtn.grid(row=0, column=3)
    	###########
    	#Interface#
    	###########
    	def interface(self):
    		self.stats1 = Button(root, text='STATS', command=None)
    		self.stats1.grid(row=1, column=0)
    		self.stats2 = Button(root, text='STATS', command=None)
    		self.stats2.grid(row=1, column=4)
    		if gi.turn == 1:
    			self.attack1 = Button(root, text='ATTACK', fg='red', command=None)
    			self.attack1.grid(row=1, column=1)
    			self.attack2 = Button(root, text='ATTACK', fg='red', command=None, state=DISABLED)
    			self.attack2.grid(row=1, column=5)
    		if gi.turn == 2:
    			self.attack1 = Button(root, text='ATTACK', fg='red', command=None, state=DISABLED)
    			self.attack1.grid(row=1, column=1)
    			self.attack2 = Button(root, text='ATTACK', fg='red', command=None)
    			self.attack2.grid(row=1, column=5)
    
    root = Tk()
    root.title('Arena I')
    app = App(None)
    root.mainloop()
    The problem occurs under #Interface#. I am using the .grid geometry manager and it is giving me weird outputs. When I run the program, and click "GO!", it gives me something like this:

    (attached)

    anyway, I am wondering why the three buttons are outside of the specified place in the .grid-ing. And strangely, why is the one button inside? I am very confused, so help out please!

    Thanks in advance!
    Attached Files
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You are creating a frame to hold grid row 0, but you are adding grid row 1 to the root window. Add them into the same frame as row 0 and they should align properly.

    Comment

    • 360monkey
      New Member
      • Feb 2010
      • 17

      #3
      Response

      So i should replace 'root' with frame?

      Code:
      # def interface(self):
      #         self.stats1 = Button([U]frame[/U], text='STATS', command=None)
      #         self.stats1.grid(row=1, column=0)
      #         self.stats2 = Button([U]frame[/U], text='STATS', command=None)
      #         self.stats2.grid(row=1, column=4)
      #         if gi.turn == 1:
      #             self.attack1 = Button([U]frame[/U], text='ATTACK', fg='red', command=None)
      #             self.attack1.grid(row=1, column=1)
      #             self.attack2 = Button([U]frame[/U], text='ATTACK', fg='red', command=None, state=DISABLED)
      #             self.attack2.grid(row=1, column=5)
      #         if gi.turn == 2:
      #             self.attack1 = Button(r[U]frame[/U], text='ATTACK', fg='red', command=None, state=DISABLED)
      #             self.attack1.grid(row=1, column=1)
      #             self.attack2 = Button([U]frame[/U], text='ATTACK', fg='red', command=None)
      #             self.attack2.grid(row=1, column=5)

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        You will need to make frame available to your method:
        Code:
                self.frame = frame
        Then replace root with self.frame.

        Comment

        • 360monkey
          New Member
          • Feb 2010
          • 17

          #5
          Response

          Bvdet,

          I put
          Code:
          self.frame = frame
          into my code, and it gave me a message saying:

          NameError: global name 'frame' is not defined
          and it points me to the self.frame = frame code.

          Code:
          def interface(self):
          		self.frame = frame
          		self.stats1 = Button(self.frame, text='STATS', command=None)
          		self.stats1.grid(row=1, column=0)
          		self.stats2 = Button(self.frame, text='STATS', command=None)
          		self.stats2.grid(row=1, column=4)
          did I introduce the code incorrectly?

          Sorry for continuing to bug you, and Thanks for continuing to help me out!

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Like this:
            Code:
            from Tkinter import *
            import random
            import gameinclude
            gi = gameinclude
             
            class App:
             
                def __init__(self, master):
                ##########
                #Menu Bar#
                ##########
                    frame = Frame(master)
                    frame.grid()
                    self.frame = frame
            Code:
                ###########
                #Interface#
                ###########
                def interface(self):
                    self.stats1 = Button(self.frame, text='STATS', command=None)
                    self.stats1.grid(row=1, column=0)

            Comment

            • 360monkey
              New Member
              • Feb 2010
              • 17

              #7
              Thank You very much!
              Your answer has increased my python knowledge and I am grateful for you consistently imparting to me great knowledge!
              Thanks!

              Comment

              Working...