A Python tkinter smiley face with buttons that change gestures
up vote
-1
down vote
favorite
Update:
I've managed to create functions for my code and a constructor. The only difficulty I've encountered is whenever I run my program, there's an error name 'SmileyFace' is not defined
when I believe I've defined it. I need help as to why SmileyFace
isn't defined. Thanks
from tkinter import *
class SmileyFace:
def __init__(self,righteye,mouth):
self.righteye=righteye
self.mouth=mouth
def smile(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = 'black')
c.delete(mouth)
mouth = c.create_arc(125, 225, 275, 275, start = 0, extent = -180, width = 5, fill = "white")
def sad(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(mouth)
mouth = c.create_arc(125, 250, 275, 300, start = 0, extent = 180, width = 5, fill = "white")
def wink(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_line(225, 140, 250, 165, 275, 140, width = 5, smooth = "true")
c.delete(mouth)
mouth = c.create_line(125, 250, 275, 250, width = 5)
def grin(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(mouth)
mouth = c.create_line(125, 250, 200, 250, 275, 215, width = 5, smooth = "true")
def main():
global c
win = Tk()
c = Canvas(win, width = 800, height = 800)
c.pack()
c.create_oval(100, 100, 350, 350, outline = "black", fill = "yellow")
eye1 = c.create_oval(125, 125, 175, 175, fill = "black")
eye2 = c.create_oval(225, 125, 275, 175, fill = "black")
mouth = c.create_line(125, 250, 275, 250, width = 5)
Smiley = SmileyFace(righteye,mouth)
Button(win,text='Smile',command=Smiley.smile).pack
Button(win, text = "Sad", command = Smiley.sad).pack
Button(win, text = "Wink", command = Smiley.wink).pack
Button(win, text = "Grin", command = Smiley.grin).pack
Button(win, text = "Quit", command = win.destroy).pack
main()
python canvas tkinter
add a comment |
up vote
-1
down vote
favorite
Update:
I've managed to create functions for my code and a constructor. The only difficulty I've encountered is whenever I run my program, there's an error name 'SmileyFace' is not defined
when I believe I've defined it. I need help as to why SmileyFace
isn't defined. Thanks
from tkinter import *
class SmileyFace:
def __init__(self,righteye,mouth):
self.righteye=righteye
self.mouth=mouth
def smile(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = 'black')
c.delete(mouth)
mouth = c.create_arc(125, 225, 275, 275, start = 0, extent = -180, width = 5, fill = "white")
def sad(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(mouth)
mouth = c.create_arc(125, 250, 275, 300, start = 0, extent = 180, width = 5, fill = "white")
def wink(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_line(225, 140, 250, 165, 275, 140, width = 5, smooth = "true")
c.delete(mouth)
mouth = c.create_line(125, 250, 275, 250, width = 5)
def grin(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(mouth)
mouth = c.create_line(125, 250, 200, 250, 275, 215, width = 5, smooth = "true")
def main():
global c
win = Tk()
c = Canvas(win, width = 800, height = 800)
c.pack()
c.create_oval(100, 100, 350, 350, outline = "black", fill = "yellow")
eye1 = c.create_oval(125, 125, 175, 175, fill = "black")
eye2 = c.create_oval(225, 125, 275, 175, fill = "black")
mouth = c.create_line(125, 250, 275, 250, width = 5)
Smiley = SmileyFace(righteye,mouth)
Button(win,text='Smile',command=Smiley.smile).pack
Button(win, text = "Sad", command = Smiley.sad).pack
Button(win, text = "Wink", command = Smiley.wink).pack
Button(win, text = "Grin", command = Smiley.grin).pack
Button(win, text = "Quit", command = win.destroy).pack
main()
python canvas tkinter
"tips and suggestions" is off topic for stackoverflow. You need to ask about a very specific problem, and explain how the code you have produces a different result than what you want.
– Bryan Oakley
Nov 11 at 4:00
Okay thanks for the clarification. I just wondered why the specific functions I've defined for the four gestures haven't managed to do anything. The code I've produced when running only displays the smileyface with the five buttons. When clicked the buttons don't change the current facial gesture, so for example the smileyface would not change once I clicked the wink button.
– Justin Paul
Nov 11 at 4:16
1
You should edit your question to say what you wrote in the comments. That's a specific problem we might be able to help with. Though, it would really help if you took the time to condense the code down to a Minimal, Complete, and Verifiable example. Often, the act of doing that will help you solve the problem by yourself.
– Bryan Oakley
Nov 11 at 4:55
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Update:
I've managed to create functions for my code and a constructor. The only difficulty I've encountered is whenever I run my program, there's an error name 'SmileyFace' is not defined
when I believe I've defined it. I need help as to why SmileyFace
isn't defined. Thanks
from tkinter import *
class SmileyFace:
def __init__(self,righteye,mouth):
self.righteye=righteye
self.mouth=mouth
def smile(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = 'black')
c.delete(mouth)
mouth = c.create_arc(125, 225, 275, 275, start = 0, extent = -180, width = 5, fill = "white")
def sad(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(mouth)
mouth = c.create_arc(125, 250, 275, 300, start = 0, extent = 180, width = 5, fill = "white")
def wink(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_line(225, 140, 250, 165, 275, 140, width = 5, smooth = "true")
c.delete(mouth)
mouth = c.create_line(125, 250, 275, 250, width = 5)
def grin(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(mouth)
mouth = c.create_line(125, 250, 200, 250, 275, 215, width = 5, smooth = "true")
def main():
global c
win = Tk()
c = Canvas(win, width = 800, height = 800)
c.pack()
c.create_oval(100, 100, 350, 350, outline = "black", fill = "yellow")
eye1 = c.create_oval(125, 125, 175, 175, fill = "black")
eye2 = c.create_oval(225, 125, 275, 175, fill = "black")
mouth = c.create_line(125, 250, 275, 250, width = 5)
Smiley = SmileyFace(righteye,mouth)
Button(win,text='Smile',command=Smiley.smile).pack
Button(win, text = "Sad", command = Smiley.sad).pack
Button(win, text = "Wink", command = Smiley.wink).pack
Button(win, text = "Grin", command = Smiley.grin).pack
Button(win, text = "Quit", command = win.destroy).pack
main()
python canvas tkinter
Update:
I've managed to create functions for my code and a constructor. The only difficulty I've encountered is whenever I run my program, there's an error name 'SmileyFace' is not defined
when I believe I've defined it. I need help as to why SmileyFace
isn't defined. Thanks
from tkinter import *
class SmileyFace:
def __init__(self,righteye,mouth):
self.righteye=righteye
self.mouth=mouth
def smile(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = 'black')
c.delete(mouth)
mouth = c.create_arc(125, 225, 275, 275, start = 0, extent = -180, width = 5, fill = "white")
def sad(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(mouth)
mouth = c.create_arc(125, 250, 275, 300, start = 0, extent = 180, width = 5, fill = "white")
def wink(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_line(225, 140, 250, 165, 275, 140, width = 5, smooth = "true")
c.delete(mouth)
mouth = c.create_line(125, 250, 275, 250, width = 5)
def grin(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(mouth)
mouth = c.create_line(125, 250, 200, 250, 275, 215, width = 5, smooth = "true")
def main():
global c
win = Tk()
c = Canvas(win, width = 800, height = 800)
c.pack()
c.create_oval(100, 100, 350, 350, outline = "black", fill = "yellow")
eye1 = c.create_oval(125, 125, 175, 175, fill = "black")
eye2 = c.create_oval(225, 125, 275, 175, fill = "black")
mouth = c.create_line(125, 250, 275, 250, width = 5)
Smiley = SmileyFace(righteye,mouth)
Button(win,text='Smile',command=Smiley.smile).pack
Button(win, text = "Sad", command = Smiley.sad).pack
Button(win, text = "Wink", command = Smiley.wink).pack
Button(win, text = "Grin", command = Smiley.grin).pack
Button(win, text = "Quit", command = win.destroy).pack
main()
python canvas tkinter
python canvas tkinter
edited Nov 13 at 6:05
cdlane
16.8k21043
16.8k21043
asked Nov 11 at 2:19
Justin Paul
12
12
"tips and suggestions" is off topic for stackoverflow. You need to ask about a very specific problem, and explain how the code you have produces a different result than what you want.
– Bryan Oakley
Nov 11 at 4:00
Okay thanks for the clarification. I just wondered why the specific functions I've defined for the four gestures haven't managed to do anything. The code I've produced when running only displays the smileyface with the five buttons. When clicked the buttons don't change the current facial gesture, so for example the smileyface would not change once I clicked the wink button.
– Justin Paul
Nov 11 at 4:16
1
You should edit your question to say what you wrote in the comments. That's a specific problem we might be able to help with. Though, it would really help if you took the time to condense the code down to a Minimal, Complete, and Verifiable example. Often, the act of doing that will help you solve the problem by yourself.
– Bryan Oakley
Nov 11 at 4:55
add a comment |
"tips and suggestions" is off topic for stackoverflow. You need to ask about a very specific problem, and explain how the code you have produces a different result than what you want.
– Bryan Oakley
Nov 11 at 4:00
Okay thanks for the clarification. I just wondered why the specific functions I've defined for the four gestures haven't managed to do anything. The code I've produced when running only displays the smileyface with the five buttons. When clicked the buttons don't change the current facial gesture, so for example the smileyface would not change once I clicked the wink button.
– Justin Paul
Nov 11 at 4:16
1
You should edit your question to say what you wrote in the comments. That's a specific problem we might be able to help with. Though, it would really help if you took the time to condense the code down to a Minimal, Complete, and Verifiable example. Often, the act of doing that will help you solve the problem by yourself.
– Bryan Oakley
Nov 11 at 4:55
"tips and suggestions" is off topic for stackoverflow. You need to ask about a very specific problem, and explain how the code you have produces a different result than what you want.
– Bryan Oakley
Nov 11 at 4:00
"tips and suggestions" is off topic for stackoverflow. You need to ask about a very specific problem, and explain how the code you have produces a different result than what you want.
– Bryan Oakley
Nov 11 at 4:00
Okay thanks for the clarification. I just wondered why the specific functions I've defined for the four gestures haven't managed to do anything. The code I've produced when running only displays the smileyface with the five buttons. When clicked the buttons don't change the current facial gesture, so for example the smileyface would not change once I clicked the wink button.
– Justin Paul
Nov 11 at 4:16
Okay thanks for the clarification. I just wondered why the specific functions I've defined for the four gestures haven't managed to do anything. The code I've produced when running only displays the smileyface with the five buttons. When clicked the buttons don't change the current facial gesture, so for example the smileyface would not change once I clicked the wink button.
– Justin Paul
Nov 11 at 4:16
1
1
You should edit your question to say what you wrote in the comments. That's a specific problem we might be able to help with. Though, it would really help if you took the time to condense the code down to a Minimal, Complete, and Verifiable example. Often, the act of doing that will help you solve the problem by yourself.
– Bryan Oakley
Nov 11 at 4:55
You should edit your question to say what you wrote in the comments. That's a specific problem we might be able to help with. Though, it would really help if you took the time to condense the code down to a Minimal, Complete, and Verifiable example. Often, the act of doing that will help you solve the problem by yourself.
– Bryan Oakley
Nov 11 at 4:55
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
The only difficulty i've encountered is whenever i run my program,
there's an error name 'SmileyFace' is not defined ...
When I try to run it, after fixing the indentation problems, I get NameError: name 'righteye' is not defined
. After fixing that, nothing happens at all when I run it due to a lack of a win.mainloop()
at the end. After fixing that, I don't see any buttons due to calls to .pack
instead of .pack()
. And so on.
You've structured this program poorly. The SmileyFace
constructor should have drawn the basic face and saved the right eye and mouth objects as properties instead of trying to deal with them as global variables. Below, I take an even simpler approach which is to use tags to avoid properties and global variables alltogether:
from tkinter import *
class SmileyFace:
def __init__(self, canvas):
self.canvas = canvas
canvas.create_oval(70, 70, 350, 350, fill='yellow')
canvas.create_oval(125, 125, 175, 175, fill='black', tags='left')
canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def smile(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 225, 275, 275, extent=-180, width=5, fill='white', tags='mouth')
def sad(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 250, 275, 300, extent=180, width=5, fill='white', tags='mouth')
def wink(self):
self.canvas.delete('right||mouth')
self.canvas.create_line(225, 140, 250, 165, 275, 140, width=5, smooth='true', tags='right')
self.canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def grin(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_line(125, 250, 200, 250, 275, 215, width=5, smooth='true', tags='mouth')
def main():
win = Tk()
canvas = Canvas(win, width=800, height=800)
canvas.pack()
smiley = SmileyFace(canvas)
Button(win, text='Smile', command=smiley.smile).pack()
Button(win, text='Sad', command=smiley.sad).pack()
Button(win, text='Wink', command=smiley.wink).pack()
Button(win, text='Grin', command=smiley.grin).pack()
Button(win, text='Quit', command=win.destroy).pack()
win.mainloop()
main()
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%2f53245288%2fa-python-tkinter-smiley-face-with-buttons-that-change-gestures%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
up vote
0
down vote
The only difficulty i've encountered is whenever i run my program,
there's an error name 'SmileyFace' is not defined ...
When I try to run it, after fixing the indentation problems, I get NameError: name 'righteye' is not defined
. After fixing that, nothing happens at all when I run it due to a lack of a win.mainloop()
at the end. After fixing that, I don't see any buttons due to calls to .pack
instead of .pack()
. And so on.
You've structured this program poorly. The SmileyFace
constructor should have drawn the basic face and saved the right eye and mouth objects as properties instead of trying to deal with them as global variables. Below, I take an even simpler approach which is to use tags to avoid properties and global variables alltogether:
from tkinter import *
class SmileyFace:
def __init__(self, canvas):
self.canvas = canvas
canvas.create_oval(70, 70, 350, 350, fill='yellow')
canvas.create_oval(125, 125, 175, 175, fill='black', tags='left')
canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def smile(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 225, 275, 275, extent=-180, width=5, fill='white', tags='mouth')
def sad(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 250, 275, 300, extent=180, width=5, fill='white', tags='mouth')
def wink(self):
self.canvas.delete('right||mouth')
self.canvas.create_line(225, 140, 250, 165, 275, 140, width=5, smooth='true', tags='right')
self.canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def grin(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_line(125, 250, 200, 250, 275, 215, width=5, smooth='true', tags='mouth')
def main():
win = Tk()
canvas = Canvas(win, width=800, height=800)
canvas.pack()
smiley = SmileyFace(canvas)
Button(win, text='Smile', command=smiley.smile).pack()
Button(win, text='Sad', command=smiley.sad).pack()
Button(win, text='Wink', command=smiley.wink).pack()
Button(win, text='Grin', command=smiley.grin).pack()
Button(win, text='Quit', command=win.destroy).pack()
win.mainloop()
main()
add a comment |
up vote
0
down vote
The only difficulty i've encountered is whenever i run my program,
there's an error name 'SmileyFace' is not defined ...
When I try to run it, after fixing the indentation problems, I get NameError: name 'righteye' is not defined
. After fixing that, nothing happens at all when I run it due to a lack of a win.mainloop()
at the end. After fixing that, I don't see any buttons due to calls to .pack
instead of .pack()
. And so on.
You've structured this program poorly. The SmileyFace
constructor should have drawn the basic face and saved the right eye and mouth objects as properties instead of trying to deal with them as global variables. Below, I take an even simpler approach which is to use tags to avoid properties and global variables alltogether:
from tkinter import *
class SmileyFace:
def __init__(self, canvas):
self.canvas = canvas
canvas.create_oval(70, 70, 350, 350, fill='yellow')
canvas.create_oval(125, 125, 175, 175, fill='black', tags='left')
canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def smile(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 225, 275, 275, extent=-180, width=5, fill='white', tags='mouth')
def sad(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 250, 275, 300, extent=180, width=5, fill='white', tags='mouth')
def wink(self):
self.canvas.delete('right||mouth')
self.canvas.create_line(225, 140, 250, 165, 275, 140, width=5, smooth='true', tags='right')
self.canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def grin(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_line(125, 250, 200, 250, 275, 215, width=5, smooth='true', tags='mouth')
def main():
win = Tk()
canvas = Canvas(win, width=800, height=800)
canvas.pack()
smiley = SmileyFace(canvas)
Button(win, text='Smile', command=smiley.smile).pack()
Button(win, text='Sad', command=smiley.sad).pack()
Button(win, text='Wink', command=smiley.wink).pack()
Button(win, text='Grin', command=smiley.grin).pack()
Button(win, text='Quit', command=win.destroy).pack()
win.mainloop()
main()
add a comment |
up vote
0
down vote
up vote
0
down vote
The only difficulty i've encountered is whenever i run my program,
there's an error name 'SmileyFace' is not defined ...
When I try to run it, after fixing the indentation problems, I get NameError: name 'righteye' is not defined
. After fixing that, nothing happens at all when I run it due to a lack of a win.mainloop()
at the end. After fixing that, I don't see any buttons due to calls to .pack
instead of .pack()
. And so on.
You've structured this program poorly. The SmileyFace
constructor should have drawn the basic face and saved the right eye and mouth objects as properties instead of trying to deal with them as global variables. Below, I take an even simpler approach which is to use tags to avoid properties and global variables alltogether:
from tkinter import *
class SmileyFace:
def __init__(self, canvas):
self.canvas = canvas
canvas.create_oval(70, 70, 350, 350, fill='yellow')
canvas.create_oval(125, 125, 175, 175, fill='black', tags='left')
canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def smile(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 225, 275, 275, extent=-180, width=5, fill='white', tags='mouth')
def sad(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 250, 275, 300, extent=180, width=5, fill='white', tags='mouth')
def wink(self):
self.canvas.delete('right||mouth')
self.canvas.create_line(225, 140, 250, 165, 275, 140, width=5, smooth='true', tags='right')
self.canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def grin(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_line(125, 250, 200, 250, 275, 215, width=5, smooth='true', tags='mouth')
def main():
win = Tk()
canvas = Canvas(win, width=800, height=800)
canvas.pack()
smiley = SmileyFace(canvas)
Button(win, text='Smile', command=smiley.smile).pack()
Button(win, text='Sad', command=smiley.sad).pack()
Button(win, text='Wink', command=smiley.wink).pack()
Button(win, text='Grin', command=smiley.grin).pack()
Button(win, text='Quit', command=win.destroy).pack()
win.mainloop()
main()
The only difficulty i've encountered is whenever i run my program,
there's an error name 'SmileyFace' is not defined ...
When I try to run it, after fixing the indentation problems, I get NameError: name 'righteye' is not defined
. After fixing that, nothing happens at all when I run it due to a lack of a win.mainloop()
at the end. After fixing that, I don't see any buttons due to calls to .pack
instead of .pack()
. And so on.
You've structured this program poorly. The SmileyFace
constructor should have drawn the basic face and saved the right eye and mouth objects as properties instead of trying to deal with them as global variables. Below, I take an even simpler approach which is to use tags to avoid properties and global variables alltogether:
from tkinter import *
class SmileyFace:
def __init__(self, canvas):
self.canvas = canvas
canvas.create_oval(70, 70, 350, 350, fill='yellow')
canvas.create_oval(125, 125, 175, 175, fill='black', tags='left')
canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def smile(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 225, 275, 275, extent=-180, width=5, fill='white', tags='mouth')
def sad(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 250, 275, 300, extent=180, width=5, fill='white', tags='mouth')
def wink(self):
self.canvas.delete('right||mouth')
self.canvas.create_line(225, 140, 250, 165, 275, 140, width=5, smooth='true', tags='right')
self.canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def grin(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_line(125, 250, 200, 250, 275, 215, width=5, smooth='true', tags='mouth')
def main():
win = Tk()
canvas = Canvas(win, width=800, height=800)
canvas.pack()
smiley = SmileyFace(canvas)
Button(win, text='Smile', command=smiley.smile).pack()
Button(win, text='Sad', command=smiley.sad).pack()
Button(win, text='Wink', command=smiley.wink).pack()
Button(win, text='Grin', command=smiley.grin).pack()
Button(win, text='Quit', command=win.destroy).pack()
win.mainloop()
main()
answered Nov 13 at 6:00
cdlane
16.8k21043
16.8k21043
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53245288%2fa-python-tkinter-smiley-face-with-buttons-that-change-gestures%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
"tips and suggestions" is off topic for stackoverflow. You need to ask about a very specific problem, and explain how the code you have produces a different result than what you want.
– Bryan Oakley
Nov 11 at 4:00
Okay thanks for the clarification. I just wondered why the specific functions I've defined for the four gestures haven't managed to do anything. The code I've produced when running only displays the smileyface with the five buttons. When clicked the buttons don't change the current facial gesture, so for example the smileyface would not change once I clicked the wink button.
– Justin Paul
Nov 11 at 4:16
1
You should edit your question to say what you wrote in the comments. That's a specific problem we might be able to help with. Though, it would really help if you took the time to condense the code down to a Minimal, Complete, and Verifiable example. Often, the act of doing that will help you solve the problem by yourself.
– Bryan Oakley
Nov 11 at 4:55