#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0

    Tkinter Grid Manager


    Having a bit of trouble getting these widgets to layout correctly. I'm trying to use the Grid Manager. I read it can be used within the Master window instead of creating a bunch of frames. For some reason however the layout isn't turning out as expected.
    I put the comments in the code to show where I would like them. Any help would be appreciated as it's my first foray into GUI development.
    Thanks

    Code:
     
    #!/usr/bin/evn python
    
    from Tkinter import *
    
    class acme_player(object):
    
       def __init__(self):
          self.acme_win = Tk()
          self.acme_win.geometry("600x400")#(x,y) == colums x rows
          self.acme_win.title("ACME MEDIA PLAYER")
    
          #should be located in top right hand corner
          self.top_search_button = Entry(self.acme_win, width = 20)
          self.top_search_button.grid(row=1, column = 600,sticky=E)
    
          self.acme_tag = Listbox(self.acme_win, width = 10)
          for tags in ["choice1", "choice2", "choice3"]:
             self.acme_tag.insert(END, tags)
          self.acme_tag.grid(row=0, column=0)
    
          #should start last row of listbox....
          self.music_btn = Button(self.acme_win, text="MUSIC", command="", activeforeground="white")
          self.music_btn.grid(row=0, column=10)
    
          self.video_btn = Button(self.acme_win, text="VIDEO", command="", activeforeground="white")
          self.video_btn.grid(row=0, column=30)
    
          self.pics_btn = Button(self.acme_win, text="PICS", command="", activeforeground="white")
          self.pics_btn.grid(row=0, column=40)
    
          #should start at edge around middle of listbox... 
          self.playing_label = Label(self.acme_win, fg="white", text="Road Runner - Roots")
          #self.playing_label.grid(row = 5, column = 10)
    
    if __name__== "__main__" :
       player = acme_player()
       mainloop()
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    I don't have access to a platform with Tkinter at the moment but I have an idea. The grid doesn't really exist until it's populated. So if you have 5 columns, you should have them as 0-4, etc.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    In Tkinter empty rows and columns are ignored, see this page and click "grid". The trick used is to add an empty label, using the width parameter to make it the correct size to fill the void.
    Code:
    from Tkinter import *
    
    class acme_player(object):
    
       def __init__(self):
          self.acme_win = Tk()
          self.acme_win.geometry("600x400")#(x,y) == colums x rows
          self.acme_win.title("ACME MEDIA PLAYER")
    
          # add empty label to extend the columns
          # note that width= depends on the font being used
          lab = Label(self.acme_win, width=20).grid(row=0, column=100)
    
          #should be located in top right hand corner
          self.top_search_button = Entry(self.acme_win, width = 20)
          self.top_search_button.grid(row=0, column = 600,sticky=E)
    
          self.acme_tag = Listbox(self.acme_win, width = 10)
          for tags in ["choice1", "choice2", "choice3"]:
             self.acme_tag.insert(END, tags)
          self.acme_tag.grid(row=1, column=0)
    
          self.create_buttons()
    
          self.acme_win.mainloop()
    
       def create_buttons(self):
          """ create all buttons
              the button id is not used anywhere so there is no reason to keep it
          """
          text_list= ["MUSIC", "VIDEO", "PICS"]
          col=10
          for ctr in range(3):
             btn = Button(self.acme_win, text=text_list[ctr], activeforeground="white")
             btn.grid(row=1, column=col)
             col += 10
    
    if __name__== "__main__" :
       player = acme_player()
    Last edited by dwblas; February 24th, 2013 at 02:33 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    Originally Posted by dwblas
    In Tkinter empty rows and columns are ignored, see this page and click "grid". The trick used is to add an empty label, using the width parameter to make it the correct size to fill the void.
    Code:
    from Tkinter import *
    
    class acme_player(object):
    
       def __init__(self):
          self.acme_win = Tk()
          self.acme_win.geometry("600x400")#(x,y) == colums x rows
          self.acme_win.title("ACME MEDIA PLAYER")
    
          # add empty label to extend the columns
          # note that width= depends on the font being used
          lab = Label(self.acme_win, width=20).grid(row=0, column=100)
    
          #should be located in top right hand corner
          self.top_search_button = Entry(self.acme_win, width = 20)
          self.top_search_button.grid(row=0, column = 600,sticky=E)
    
          self.acme_tag = Listbox(self.acme_win, width = 10)
          for tags in ["choice1", "choice2", "choice3"]:
             self.acme_tag.insert(END, tags)
          self.acme_tag.grid(row=1, column=0)
    
          self.create_buttons()
    
          self.acme_win.mainloop()
    
       def create_buttons(self):
          """ create all buttons
              the button id is not used anywhere so there is no reason to keep it
          """
          text_list= ["MUSIC", "VIDEO", "PICS"]
          col=10
          for ctr in range(3):
             btn = Button(self.acme_win, text=text_list[ctr], activeforeground="white")
             btn.grid(row=1, column=col)
             col += 10
    
    if __name__== "__main__" :
       player = acme_player()
    dwblas, thank you very much fore your help and reply. Unforutunely there are still a few questions. Which row was empty that was being ignored?

    Also I swear I fixed it last night [by adding a text label ]after I posted and it was fine, however seems I made some changes and it is now no longer being rendered at top right where I want it to. Does it's place depend on where the other widgets are? Please let me know how I can achive the desired results.

    Also as you can see I like the pythonic way of creating the other buttons and I copied it... I haven't called the method for that as yet...that will be another problem I bother you with.

    thank you kindly,

    Code:
    #!/usr/bin/evn python
    
    from Tkinter import *
    
    class acme_player(object):
    
       def __init__(self):
          self.acme_win = Tk()
          #geometry is columns (across) by num_of_rows (up/down)
          self.acme_win.geometry("600x400")
          self.acme_win.title("ACME MEDIA PLAYER")
          
          #should be located in top right hand corner
          self.search_btn_toggler = Label(self.acme_win, text="Search Box",fg="blue")
          self.search_btn_toggler.grid(row=0,column=550,sticky=NE)
    
          self.top_search_entry = Entry(self.acme_win, width = 20)
          self.top_search_entry.grid(row=0,column=600,sticky=NE)
    
          self.acme_tag = Listbox(self.acme_win, width = 10)
          for tags in ["Choice1", "Choice2", "Choice3","Choice4", "Choice5", "Choice6","Choice7", "Choice8", "Choice9"]:
             self.acme_tag.insert(END, tags)
          self.acme_tag.grid(row=0, column=0)
    
          #should start at last row of listbox...
          #self.setup_media_btns()
         
          self.acme_win.mainloop()
          
       def setup_media_btns(self):
          
          media_type = ["MUSIC", "", "VIDEO", "", "PICS"]
          num_of_media = len(media_type)
          media_clm =5 
    
          for media in range(num_of_media):
             STATE = "disabled" if media_type[media] == "" else "active"
             btn = Button(self.acme_win, width=8, state=STATE, text=media_type[media], activeforeground="white") 
             btn.grid(row=10, column = media_clm)
             media_clm +=10 
    
    
       
    
    if __name__== "__main__" :
       player = acme_player()
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    Your removed the "width=" from the label, which made it large enough to move the Entry box to the corner. You can always re-check the original post above.
    Last edited by dwblas; February 25th, 2013 at 01:33 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    Originally Posted by dwblas
    Your removed the "width=" from the label, which made it large enough to move the Entry box to the corner. You can always re-check the original post above.
    dwblas, totally forgot to say thanks for your help with this. I'm still learing about the grid manager and have figured out some thing but thank you for your help sorry it took so long.
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."


IMN logo majestic logo threadwatch logo seochat tools logo