Widgets not showing up in Tkinter GUI
I created code in order to display a 3x3 grid of buttons for a Tic-Tac-Toe program i'm developing. The grid worked before but when i tried to put the code into a class i just get a blank screen when i try to run the program. This is my code:
from tkinter import *
buttons = {".!frame.!button": 0,
".!frame.!button2": 1,
".!frame.!button3": 2,
".!frame.!button4": 3,
".!frame.!button5": 4,
".!frame.!button6": 5,
".!frame.!button7": 6,
".!frame.!button8": 7,
".!frame.!button9": 8,
}
class GameBoard:
def __init__(self, master):
self.field = Frame(master)
self.field.grid
self.b1 = Button(self.field, text="-")
self.b1.bind("<Button-1>", self.setfield)
self.b1.grid(row=0, column=0)
self.b2 = Button(self.field, text="-")
self.b2.bind("<Button-1>", self.setfield)
self.b2.grid(row=0, column=1)
self.b3 = Button(self.field, text="-")
self.b3.bind("<Button-1>", self.setfield)
self.b3.grid(row=0, column=2)
self.b4 = Button(self.field, text="-")
self.b4.bind("<Button-1>", self.setfield)
self.b4.grid(row=1, column=0)
self.b5 = Button(self.field, text="-")
self.b5.bind("<Button-1>", self.setfield)
self.b5.grid(row=1, column=1)
self.b6 = Button(self.field, text="-")
self.b6.bind("<Button-1>", self.setfield)
self.b6.grid(row=1, column=2)
self.b7 = Button(self.field, text="-")
self.b7.bind("<Button-1>", self.setfield)
self.b7.grid(row=2, column=0)
self.b8 = Button(self.field, text="-")
self.b8.bind("<Button-1>", self.setfield)
self.b8.grid(row=2, column=1)
self.b9 = Button(self.field, text="-")
self.b9.bind("<Button-1>", self.setfield)
self.b9.grid(row=2, column=2)
def setfield(self, event):
print(buttons[str(event.widget)])
root = Tk()
board = GameBoard(root)
root.mainloop()
Could someone help me find out why i just get an empty frame when i run the program?
python class user-interface tkinter
add a comment |
I created code in order to display a 3x3 grid of buttons for a Tic-Tac-Toe program i'm developing. The grid worked before but when i tried to put the code into a class i just get a blank screen when i try to run the program. This is my code:
from tkinter import *
buttons = {".!frame.!button": 0,
".!frame.!button2": 1,
".!frame.!button3": 2,
".!frame.!button4": 3,
".!frame.!button5": 4,
".!frame.!button6": 5,
".!frame.!button7": 6,
".!frame.!button8": 7,
".!frame.!button9": 8,
}
class GameBoard:
def __init__(self, master):
self.field = Frame(master)
self.field.grid
self.b1 = Button(self.field, text="-")
self.b1.bind("<Button-1>", self.setfield)
self.b1.grid(row=0, column=0)
self.b2 = Button(self.field, text="-")
self.b2.bind("<Button-1>", self.setfield)
self.b2.grid(row=0, column=1)
self.b3 = Button(self.field, text="-")
self.b3.bind("<Button-1>", self.setfield)
self.b3.grid(row=0, column=2)
self.b4 = Button(self.field, text="-")
self.b4.bind("<Button-1>", self.setfield)
self.b4.grid(row=1, column=0)
self.b5 = Button(self.field, text="-")
self.b5.bind("<Button-1>", self.setfield)
self.b5.grid(row=1, column=1)
self.b6 = Button(self.field, text="-")
self.b6.bind("<Button-1>", self.setfield)
self.b6.grid(row=1, column=2)
self.b7 = Button(self.field, text="-")
self.b7.bind("<Button-1>", self.setfield)
self.b7.grid(row=2, column=0)
self.b8 = Button(self.field, text="-")
self.b8.bind("<Button-1>", self.setfield)
self.b8.grid(row=2, column=1)
self.b9 = Button(self.field, text="-")
self.b9.bind("<Button-1>", self.setfield)
self.b9.grid(row=2, column=2)
def setfield(self, event):
print(buttons[str(event.widget)])
root = Tk()
board = GameBoard(root)
root.mainloop()
Could someone help me find out why i just get an empty frame when i run the program?
python class user-interface tkinter
1
self.field.grid
does nothing without adding()
at the end...
– jasonharper
Nov 21 '18 at 2:51
Well that's embarassing hahahah
– Leke
Nov 21 '18 at 2:53
What are you trying to do withbuttons
? There's no guarantee that the strings you entered will be the internal name of the buttons. It's definitely not the name in python 2.x.
– Bryan Oakley
Nov 21 '18 at 3:04
@BryanOakley i wanted each button to return a value depending on its position in the grid without making an individual method for each one as i don't know how to pass arguments into the setfield method. I figured there was no guarantee it would stay the same depending on version etc but it was the only thing i could think of (for now anyways)
– Leke
Nov 21 '18 at 3:24
add a comment |
I created code in order to display a 3x3 grid of buttons for a Tic-Tac-Toe program i'm developing. The grid worked before but when i tried to put the code into a class i just get a blank screen when i try to run the program. This is my code:
from tkinter import *
buttons = {".!frame.!button": 0,
".!frame.!button2": 1,
".!frame.!button3": 2,
".!frame.!button4": 3,
".!frame.!button5": 4,
".!frame.!button6": 5,
".!frame.!button7": 6,
".!frame.!button8": 7,
".!frame.!button9": 8,
}
class GameBoard:
def __init__(self, master):
self.field = Frame(master)
self.field.grid
self.b1 = Button(self.field, text="-")
self.b1.bind("<Button-1>", self.setfield)
self.b1.grid(row=0, column=0)
self.b2 = Button(self.field, text="-")
self.b2.bind("<Button-1>", self.setfield)
self.b2.grid(row=0, column=1)
self.b3 = Button(self.field, text="-")
self.b3.bind("<Button-1>", self.setfield)
self.b3.grid(row=0, column=2)
self.b4 = Button(self.field, text="-")
self.b4.bind("<Button-1>", self.setfield)
self.b4.grid(row=1, column=0)
self.b5 = Button(self.field, text="-")
self.b5.bind("<Button-1>", self.setfield)
self.b5.grid(row=1, column=1)
self.b6 = Button(self.field, text="-")
self.b6.bind("<Button-1>", self.setfield)
self.b6.grid(row=1, column=2)
self.b7 = Button(self.field, text="-")
self.b7.bind("<Button-1>", self.setfield)
self.b7.grid(row=2, column=0)
self.b8 = Button(self.field, text="-")
self.b8.bind("<Button-1>", self.setfield)
self.b8.grid(row=2, column=1)
self.b9 = Button(self.field, text="-")
self.b9.bind("<Button-1>", self.setfield)
self.b9.grid(row=2, column=2)
def setfield(self, event):
print(buttons[str(event.widget)])
root = Tk()
board = GameBoard(root)
root.mainloop()
Could someone help me find out why i just get an empty frame when i run the program?
python class user-interface tkinter
I created code in order to display a 3x3 grid of buttons for a Tic-Tac-Toe program i'm developing. The grid worked before but when i tried to put the code into a class i just get a blank screen when i try to run the program. This is my code:
from tkinter import *
buttons = {".!frame.!button": 0,
".!frame.!button2": 1,
".!frame.!button3": 2,
".!frame.!button4": 3,
".!frame.!button5": 4,
".!frame.!button6": 5,
".!frame.!button7": 6,
".!frame.!button8": 7,
".!frame.!button9": 8,
}
class GameBoard:
def __init__(self, master):
self.field = Frame(master)
self.field.grid
self.b1 = Button(self.field, text="-")
self.b1.bind("<Button-1>", self.setfield)
self.b1.grid(row=0, column=0)
self.b2 = Button(self.field, text="-")
self.b2.bind("<Button-1>", self.setfield)
self.b2.grid(row=0, column=1)
self.b3 = Button(self.field, text="-")
self.b3.bind("<Button-1>", self.setfield)
self.b3.grid(row=0, column=2)
self.b4 = Button(self.field, text="-")
self.b4.bind("<Button-1>", self.setfield)
self.b4.grid(row=1, column=0)
self.b5 = Button(self.field, text="-")
self.b5.bind("<Button-1>", self.setfield)
self.b5.grid(row=1, column=1)
self.b6 = Button(self.field, text="-")
self.b6.bind("<Button-1>", self.setfield)
self.b6.grid(row=1, column=2)
self.b7 = Button(self.field, text="-")
self.b7.bind("<Button-1>", self.setfield)
self.b7.grid(row=2, column=0)
self.b8 = Button(self.field, text="-")
self.b8.bind("<Button-1>", self.setfield)
self.b8.grid(row=2, column=1)
self.b9 = Button(self.field, text="-")
self.b9.bind("<Button-1>", self.setfield)
self.b9.grid(row=2, column=2)
def setfield(self, event):
print(buttons[str(event.widget)])
root = Tk()
board = GameBoard(root)
root.mainloop()
Could someone help me find out why i just get an empty frame when i run the program?
python class user-interface tkinter
python class user-interface tkinter
asked Nov 21 '18 at 2:47
LekeLeke
255
255
1
self.field.grid
does nothing without adding()
at the end...
– jasonharper
Nov 21 '18 at 2:51
Well that's embarassing hahahah
– Leke
Nov 21 '18 at 2:53
What are you trying to do withbuttons
? There's no guarantee that the strings you entered will be the internal name of the buttons. It's definitely not the name in python 2.x.
– Bryan Oakley
Nov 21 '18 at 3:04
@BryanOakley i wanted each button to return a value depending on its position in the grid without making an individual method for each one as i don't know how to pass arguments into the setfield method. I figured there was no guarantee it would stay the same depending on version etc but it was the only thing i could think of (for now anyways)
– Leke
Nov 21 '18 at 3:24
add a comment |
1
self.field.grid
does nothing without adding()
at the end...
– jasonharper
Nov 21 '18 at 2:51
Well that's embarassing hahahah
– Leke
Nov 21 '18 at 2:53
What are you trying to do withbuttons
? There's no guarantee that the strings you entered will be the internal name of the buttons. It's definitely not the name in python 2.x.
– Bryan Oakley
Nov 21 '18 at 3:04
@BryanOakley i wanted each button to return a value depending on its position in the grid without making an individual method for each one as i don't know how to pass arguments into the setfield method. I figured there was no guarantee it would stay the same depending on version etc but it was the only thing i could think of (for now anyways)
– Leke
Nov 21 '18 at 3:24
1
1
self.field.grid
does nothing without adding ()
at the end...– jasonharper
Nov 21 '18 at 2:51
self.field.grid
does nothing without adding ()
at the end...– jasonharper
Nov 21 '18 at 2:51
Well that's embarassing hahahah
– Leke
Nov 21 '18 at 2:53
Well that's embarassing hahahah
– Leke
Nov 21 '18 at 2:53
What are you trying to do with
buttons
? There's no guarantee that the strings you entered will be the internal name of the buttons. It's definitely not the name in python 2.x.– Bryan Oakley
Nov 21 '18 at 3:04
What are you trying to do with
buttons
? There's no guarantee that the strings you entered will be the internal name of the buttons. It's definitely not the name in python 2.x.– Bryan Oakley
Nov 21 '18 at 3:04
@BryanOakley i wanted each button to return a value depending on its position in the grid without making an individual method for each one as i don't know how to pass arguments into the setfield method. I figured there was no guarantee it would stay the same depending on version etc but it was the only thing i could think of (for now anyways)
– Leke
Nov 21 '18 at 3:24
@BryanOakley i wanted each button to return a value depending on its position in the grid without making an individual method for each one as i don't know how to pass arguments into the setfield method. I figured there was no guarantee it would stay the same depending on version etc but it was the only thing i could think of (for now anyways)
– Leke
Nov 21 '18 at 3:24
add a comment |
1 Answer
1
active
oldest
votes
Could someone help me find out why i just get an empty frame when i run the program?
It is because you aren't adding it to the window. Consider this code:
self.field.grid
It does absolutely nothing. For the window to appear you must call the function:
self.field.grid()
In my opinion a class should never call grid
or pack
or place
on itself. That should be the job of the caller. It's a good habit to get in because it promotes code reuse.
Personally I would remove that line and change a couple of the last lines to this:
board = GameBoard(root)
board.grid() # or board.pack(...)
You are making way too much work for yourself. You can pass arguments to the callback. For example:
self.b1 = Button(self.field, text="-", command=lambda: setfield(1))
With that, your callback will be called with the parameter 1
, and you won't need to do any sort of lookup.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404608%2fwidgets-not-showing-up-in-tkinter-gui%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Could someone help me find out why i just get an empty frame when i run the program?
It is because you aren't adding it to the window. Consider this code:
self.field.grid
It does absolutely nothing. For the window to appear you must call the function:
self.field.grid()
In my opinion a class should never call grid
or pack
or place
on itself. That should be the job of the caller. It's a good habit to get in because it promotes code reuse.
Personally I would remove that line and change a couple of the last lines to this:
board = GameBoard(root)
board.grid() # or board.pack(...)
You are making way too much work for yourself. You can pass arguments to the callback. For example:
self.b1 = Button(self.field, text="-", command=lambda: setfield(1))
With that, your callback will be called with the parameter 1
, and you won't need to do any sort of lookup.
add a comment |
Could someone help me find out why i just get an empty frame when i run the program?
It is because you aren't adding it to the window. Consider this code:
self.field.grid
It does absolutely nothing. For the window to appear you must call the function:
self.field.grid()
In my opinion a class should never call grid
or pack
or place
on itself. That should be the job of the caller. It's a good habit to get in because it promotes code reuse.
Personally I would remove that line and change a couple of the last lines to this:
board = GameBoard(root)
board.grid() # or board.pack(...)
You are making way too much work for yourself. You can pass arguments to the callback. For example:
self.b1 = Button(self.field, text="-", command=lambda: setfield(1))
With that, your callback will be called with the parameter 1
, and you won't need to do any sort of lookup.
add a comment |
Could someone help me find out why i just get an empty frame when i run the program?
It is because you aren't adding it to the window. Consider this code:
self.field.grid
It does absolutely nothing. For the window to appear you must call the function:
self.field.grid()
In my opinion a class should never call grid
or pack
or place
on itself. That should be the job of the caller. It's a good habit to get in because it promotes code reuse.
Personally I would remove that line and change a couple of the last lines to this:
board = GameBoard(root)
board.grid() # or board.pack(...)
You are making way too much work for yourself. You can pass arguments to the callback. For example:
self.b1 = Button(self.field, text="-", command=lambda: setfield(1))
With that, your callback will be called with the parameter 1
, and you won't need to do any sort of lookup.
Could someone help me find out why i just get an empty frame when i run the program?
It is because you aren't adding it to the window. Consider this code:
self.field.grid
It does absolutely nothing. For the window to appear you must call the function:
self.field.grid()
In my opinion a class should never call grid
or pack
or place
on itself. That should be the job of the caller. It's a good habit to get in because it promotes code reuse.
Personally I would remove that line and change a couple of the last lines to this:
board = GameBoard(root)
board.grid() # or board.pack(...)
You are making way too much work for yourself. You can pass arguments to the callback. For example:
self.b1 = Button(self.field, text="-", command=lambda: setfield(1))
With that, your callback will be called with the parameter 1
, and you won't need to do any sort of lookup.
answered Nov 21 '18 at 3:33
Bryan OakleyBryan Oakley
220k22267431
220k22267431
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404608%2fwidgets-not-showing-up-in-tkinter-gui%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
self.field.grid
does nothing without adding()
at the end...– jasonharper
Nov 21 '18 at 2:51
Well that's embarassing hahahah
– Leke
Nov 21 '18 at 2:53
What are you trying to do with
buttons
? There's no guarantee that the strings you entered will be the internal name of the buttons. It's definitely not the name in python 2.x.– Bryan Oakley
Nov 21 '18 at 3:04
@BryanOakley i wanted each button to return a value depending on its position in the grid without making an individual method for each one as i don't know how to pass arguments into the setfield method. I figured there was no guarantee it would stay the same depending on version etc but it was the only thing i could think of (for now anyways)
– Leke
Nov 21 '18 at 3:24