Hey Bytes Community!
I am currently using Tkinter to create a GUI window, and I ran into a problem. Here is my 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!
Like this: - 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
- ###########
-
#Interface#
-
###########
-
def interface(self):
-
self.stats1 = Button(self.frame, text='STATS', command=None)
-
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.
So i should replace 'root' with frame? - # def interface(self):
-
# self.stats1 = Button(frame, text='STATS', command=None)
-
# self.stats1.grid(row=1, column=0)
-
# self.stats2 = Button(frame, text='STATS', command=None)
-
# self.stats2.grid(row=1, column=4)
-
# if gi.turn == 1:
-
# self.attack1 = Button(frame, text='ATTACK', fg='red', command=None)
-
# self.attack1.grid(row=1, column=1)
-
# self.attack2 = Button(frame, text='ATTACK', fg='red', command=None, state=DISABLED)
-
# self.attack2.grid(row=1, column=5)
-
# if gi.turn == 2:
-
# self.attack1 = Button(rframe, text='ATTACK', fg='red', command=None, state=DISABLED)
-
# self.attack1.grid(row=1, column=1)
-
# self.attack2 = Button(frame, text='ATTACK', fg='red', command=None)
-
# self.attack2.grid(row=1, column=5)
bvdet 2,851
Expert Mod 2GB
You will need to make frame available to your method:
Then replace root with self.frame.
Bvdet,
I put
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. - 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!
bvdet 2,851
Expert Mod 2GB
Like this: - 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
- ###########
-
#Interface#
-
###########
-
def interface(self):
-
self.stats1 = Button(self.frame, text='STATS', command=None)
-
self.stats1.grid(row=1, column=0)
Thank You very much!
Your answer has increased my python knowledge and I am grateful for you consistently imparting to me great knowledge!
Thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |