473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Strange .grid sectioning? Tkinter?

360monkey
Hey Bytes Community!

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

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import random
  3. import gameinclude
  4. gi = gameinclude
  5.  
  6. class App:
  7.  
  8.     def __init__(self, master):
  9.     ##########
  10.     #Menu Bar#
  11.     ##########
  12.         frame = Frame(master)
  13.         frame.grid()
  14.         menu = Menu(root)
  15.         root.config(menu=menu)
  16.         filemenu = Menu(menu)
  17.         menu.add_cascade(label='File', menu=filemenu)
  18.         filemenu.add_command(label='Quit', command=frame.quit)
  19.         infomenu = Menu(menu)
  20.         menu.add_cascade(label='About...', menu=infomenu)
  21.         infomenu.add_command(label='About BattlerIII', command=None)
  22.     ############
  23.     #Name Entry#
  24.     ############
  25.         self.pone = Label(frame, text='Player One').grid(row=0, column=0)
  26.         self.epone = Entry(frame, textvariable = StringVar)
  27.         self.epone.grid(row=0, column=1)
  28.         self.ptwo = Label(frame, text='Player Two').grid(row=0, column=4)
  29.         self.eptwo = Entry(frame, textvariable = StringVar)
  30.         self.eptwo.grid(row=0, column=5)
  31.         self.activebtn = Button(frame, text='GO!', command = self.interface)
  32.         self.activebtn.grid(row=0, column=3)
  33.     ###########
  34.     #Interface#
  35.     ###########
  36.     def interface(self):
  37.         self.stats1 = Button(root, text='STATS', command=None)
  38.         self.stats1.grid(row=1, column=0)
  39.         self.stats2 = Button(root, text='STATS', command=None)
  40.         self.stats2.grid(row=1, column=4)
  41.         if gi.turn == 1:
  42.             self.attack1 = Button(root, text='ATTACK', fg='red', command=None)
  43.             self.attack1.grid(row=1, column=1)
  44.             self.attack2 = Button(root, text='ATTACK', fg='red', command=None, state=DISABLED)
  45.             self.attack2.grid(row=1, column=5)
  46.         if gi.turn == 2:
  47.             self.attack1 = Button(root, text='ATTACK', fg='red', command=None, state=DISABLED)
  48.             self.attack1.grid(row=1, column=1)
  49.             self.attack2 = Button(root, text='ATTACK', fg='red', command=None)
  50.             self.attack2.grid(row=1, column=5)
  51.  
  52. root = Tk()
  53. root.title('Arena I')
  54. app = App(None)
  55. 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 Images
File Type: jpg WTF!.jpg (7.1 KB, 153 views)
Feb 21 '10 #1

✓ answered by bvdet

Like this:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import random
  3. import gameinclude
  4. gi = gameinclude
  5.  
  6. class App:
  7.  
  8.     def __init__(self, master):
  9.     ##########
  10.     #Menu Bar#
  11.     ##########
  12.         frame = Frame(master)
  13.         frame.grid()
  14.         self.frame = frame
Expand|Select|Wrap|Line Numbers
  1.     ###########
  2.     #Interface#
  3.     ###########
  4.     def interface(self):
  5.         self.stats1 = Button(self.frame, text='STATS', command=None)
  6.         self.stats1.grid(row=1, column=0)

6 2061
bvdet
2,851 Expert Mod 2GB
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.
Feb 21 '10 #2
So i should replace 'root' with frame?

Expand|Select|Wrap|Line Numbers
  1. # def interface(self):
  2. #         self.stats1 = Button(frame, text='STATS', command=None)
  3. #         self.stats1.grid(row=1, column=0)
  4. #         self.stats2 = Button(frame, text='STATS', command=None)
  5. #         self.stats2.grid(row=1, column=4)
  6. #         if gi.turn == 1:
  7. #             self.attack1 = Button(frame, text='ATTACK', fg='red', command=None)
  8. #             self.attack1.grid(row=1, column=1)
  9. #             self.attack2 = Button(frame, text='ATTACK', fg='red', command=None, state=DISABLED)
  10. #             self.attack2.grid(row=1, column=5)
  11. #         if gi.turn == 2:
  12. #             self.attack1 = Button(rframe, text='ATTACK', fg='red', command=None, state=DISABLED)
  13. #             self.attack1.grid(row=1, column=1)
  14. #             self.attack2 = Button(frame, text='ATTACK', fg='red', command=None)
  15. #             self.attack2.grid(row=1, column=5)
Feb 21 '10 #3
bvdet
2,851 Expert Mod 2GB
You will need to make frame available to your method:
Expand|Select|Wrap|Line Numbers
  1.         self.frame = frame
Then replace root with self.frame.
Feb 21 '10 #4
Bvdet,

I put
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. def interface(self):
  2.         self.frame = frame
  3.         self.stats1 = Button(self.frame, text='STATS', command=None)
  4.         self.stats1.grid(row=1, column=0)
  5.         self.stats2 = Button(self.frame, text='STATS', command=None)
  6.         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!
Feb 21 '10 #5
bvdet
2,851 Expert Mod 2GB
Like this:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import random
  3. import gameinclude
  4. gi = gameinclude
  5.  
  6. class App:
  7.  
  8.     def __init__(self, master):
  9.     ##########
  10.     #Menu Bar#
  11.     ##########
  12.         frame = Frame(master)
  13.         frame.grid()
  14.         self.frame = frame
Expand|Select|Wrap|Line Numbers
  1.     ###########
  2.     #Interface#
  3.     ###########
  4.     def interface(self):
  5.         self.stats1 = Button(self.frame, text='STATS', command=None)
  6.         self.stats1.grid(row=1, column=0)
Feb 22 '10 #6
Thank You very much!
Your answer has increased my python knowledge and I am grateful for you consistently imparting to me great knowledge!
Thanks!
Feb 22 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
6
by: Richard Lewis | last post by:
Hi there, I've got a tree control in Tkinter (using the ESRF Tree module) but I can't get it to layout how I want it. I'd like to have it so that it streches north/south (anchored to the top...
2
by: anx | last post by:
I've got a grid-managed frame, containing a column of Labels, and a corresponding column of Entry widgets. I'd like to be able to display dozens, or even hundreds of rows, and use a vertical...
1
by: Stan Cook | last post by:
A newbie to Tkinter here. . . . . . I'm trying to set the focus on an Entry textbox with focus_set. I am using the grid manager. I created the same interface before using the pack() method...
3
by: H J van Rooyen | last post by:
Hi, Still struggling with my GUI exercise - I have the following lines of code in a routine that is bound at <Key-Returnto an instance of Entry : self.disp.Amount_des = Label(self.disp,...
3
by: aldonnelley | last post by:
Hi all. Just having a weird problem with tkinter. I'm trying to make a gui that shows results from an image search, with a "forward" and "back" button so the user can compare results from...
5
by: Mudcat | last post by:
I was trying to design a widget that I could drag and drop anywhere in a frame and then resize by pulling at the edges with the mouse. I have fiddled with several different approaches and came...
7
by: Ray | last post by:
hi, I have a question about how to use .grid_forget (in python/TK) I need to work on grid repeatly. everytime when a button is pressed, the rows of grid is different. such like, first time, it...
2
by: skanemupp | last post by:
so my little calculator works perfectly now. just having some trouble with the layout. this whole tkinter-thing seems to be more tricky than it should be. how can i make the 4 column of buttons...
1
by: Francesco Bochicchio | last post by:
Il Mon, 18 Aug 2008 12:15:10 +0100, dudeja.rajat ha scritto: Uhm, I don't think you should use the grid manager to obtain a window like that. The grid manager is for equally distributing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.