Passing variables between functions inside class for OptionMenu
I'm having a problem with how to run some piece of code on some state of OptionMenu.
from tkinter import *
class Browser(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.createUtility()
return
def createUtility(self):
self.main_frame = Frame(bg="")
self.main_frame.grid(row=0, column=0)
self.display_frame = Frame(width=50, height=50, bg="")
self.display_frame.grid(row=1, column=0)
options = ["Plants", "Wood", "Mushrooms", "Animals"]
self.variable = StringVar()
self.variable.set("Wood")
self.change_menu = OptionMenu(self.main_frame, self.variable, *options, command=self.func)
self.change_menu.grid(row=0, column=0)
self.exit_button = Button(self.display_frame, text="Quit!", command=quit)
self.exit_button.grid(row=1, column=1)
if value == "Plants":
#do some code
elif value == "Wood":
#do some code
elif value == "Mushrooms":
#do some code
elif value == "Animals":
#do some code
else:
#do some code
return
def func(self, value):
return value
#This value should be used for an if block in createUtility function
if __name__ == "__main__":
b = Browser(Tk())
mainloop()
I don't get errors, so that's good. I can click and select options in OptionMenu but nothing happens.
python-3.x function class variables tkinter
add a comment |
I'm having a problem with how to run some piece of code on some state of OptionMenu.
from tkinter import *
class Browser(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.createUtility()
return
def createUtility(self):
self.main_frame = Frame(bg="")
self.main_frame.grid(row=0, column=0)
self.display_frame = Frame(width=50, height=50, bg="")
self.display_frame.grid(row=1, column=0)
options = ["Plants", "Wood", "Mushrooms", "Animals"]
self.variable = StringVar()
self.variable.set("Wood")
self.change_menu = OptionMenu(self.main_frame, self.variable, *options, command=self.func)
self.change_menu.grid(row=0, column=0)
self.exit_button = Button(self.display_frame, text="Quit!", command=quit)
self.exit_button.grid(row=1, column=1)
if value == "Plants":
#do some code
elif value == "Wood":
#do some code
elif value == "Mushrooms":
#do some code
elif value == "Animals":
#do some code
else:
#do some code
return
def func(self, value):
return value
#This value should be used for an if block in createUtility function
if __name__ == "__main__":
b = Browser(Tk())
mainloop()
I don't get errors, so that's good. I can click and select options in OptionMenu but nothing happens.
python-3.x function class variables tkinter
I don't get errors, so that's good.... This is not always a good thing. At a glance I see some stuff I would change.
– Mike - SMT
Nov 15 '18 at 20:02
Yeah, I realised now.
– CodeMonster
Nov 16 '18 at 15:36
add a comment |
I'm having a problem with how to run some piece of code on some state of OptionMenu.
from tkinter import *
class Browser(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.createUtility()
return
def createUtility(self):
self.main_frame = Frame(bg="")
self.main_frame.grid(row=0, column=0)
self.display_frame = Frame(width=50, height=50, bg="")
self.display_frame.grid(row=1, column=0)
options = ["Plants", "Wood", "Mushrooms", "Animals"]
self.variable = StringVar()
self.variable.set("Wood")
self.change_menu = OptionMenu(self.main_frame, self.variable, *options, command=self.func)
self.change_menu.grid(row=0, column=0)
self.exit_button = Button(self.display_frame, text="Quit!", command=quit)
self.exit_button.grid(row=1, column=1)
if value == "Plants":
#do some code
elif value == "Wood":
#do some code
elif value == "Mushrooms":
#do some code
elif value == "Animals":
#do some code
else:
#do some code
return
def func(self, value):
return value
#This value should be used for an if block in createUtility function
if __name__ == "__main__":
b = Browser(Tk())
mainloop()
I don't get errors, so that's good. I can click and select options in OptionMenu but nothing happens.
python-3.x function class variables tkinter
I'm having a problem with how to run some piece of code on some state of OptionMenu.
from tkinter import *
class Browser(Frame):
def __init__(self, root):
self.root = root
super().__init__(self.root)
self.createUtility()
return
def createUtility(self):
self.main_frame = Frame(bg="")
self.main_frame.grid(row=0, column=0)
self.display_frame = Frame(width=50, height=50, bg="")
self.display_frame.grid(row=1, column=0)
options = ["Plants", "Wood", "Mushrooms", "Animals"]
self.variable = StringVar()
self.variable.set("Wood")
self.change_menu = OptionMenu(self.main_frame, self.variable, *options, command=self.func)
self.change_menu.grid(row=0, column=0)
self.exit_button = Button(self.display_frame, text="Quit!", command=quit)
self.exit_button.grid(row=1, column=1)
if value == "Plants":
#do some code
elif value == "Wood":
#do some code
elif value == "Mushrooms":
#do some code
elif value == "Animals":
#do some code
else:
#do some code
return
def func(self, value):
return value
#This value should be used for an if block in createUtility function
if __name__ == "__main__":
b = Browser(Tk())
mainloop()
I don't get errors, so that's good. I can click and select options in OptionMenu but nothing happens.
python-3.x function class variables tkinter
python-3.x function class variables tkinter
asked Nov 15 '18 at 16:44
CodeMonsterCodeMonster
125
125
I don't get errors, so that's good.... This is not always a good thing. At a glance I see some stuff I would change.
– Mike - SMT
Nov 15 '18 at 20:02
Yeah, I realised now.
– CodeMonster
Nov 16 '18 at 15:36
add a comment |
I don't get errors, so that's good.... This is not always a good thing. At a glance I see some stuff I would change.
– Mike - SMT
Nov 15 '18 at 20:02
Yeah, I realised now.
– CodeMonster
Nov 16 '18 at 15:36
I don't get errors, so that's good.... This is not always a good thing. At a glance I see some stuff I would change.
– Mike - SMT
Nov 15 '18 at 20:02
I don't get errors, so that's good.... This is not always a good thing. At a glance I see some stuff I would change.
– Mike - SMT
Nov 15 '18 at 20:02
Yeah, I realised now.
– CodeMonster
Nov 16 '18 at 15:36
Yeah, I realised now.
– CodeMonster
Nov 16 '18 at 15:36
add a comment |
1 Answer
1
active
oldest
votes
First I want to say that not getting an error does not by default mean "good". There could be many reason for not getting an error and still have problems.
Now moving on to the code itself. You have a few problems.
Your method
func
is not doing anything useful here. Returning the event back to the menu item will result in nothing as it is being ignored by theOptionMenu
. So here you would get no error because it is ignored.Your
if/elif
statement will only run once and should fail with the error:NameError: name 'value' is not defined
. Due to value never being defined in thecreateUtility
method.Your use of
return
is not needed in any of these places and in fact does nothing useful.b = Browser(Tk()) mainloop()
Is not a good approach. You need to be more explicit on your tkinter instance.Small grievance but worth mentioning. Instead of doing
from tkinter import *
you should doimport tkinter as tk
and use thetk.
prefix where needed. This will help prevent accidental overtiring of imports form tkinter.
Change it to something like this:
if __name__ == "__main__":
root = Tk()
Browser(root)
root.mainloop()
Code:
import tkinter as tk
class Browser(tk.Frame):
def __init__(self, root):
super().__init__(root)
main_frame = tk.Frame(bg="")
display_frame = tk.Frame(width=50, height=50, bg="")
main_frame.grid(row=0, column=0)
display_frame.grid(row=1, column=0)
options = ["Plants", "Wood", "Mushrooms", "Animals"]
self.variable = tk.StringVar()
self.variable.set("Wood")
self.change_menu = tk.OptionMenu(main_frame, self.variable, *options, command=self.check_value)
self.exit_button = tk.Button(display_frame, text="Quit!", command=quit)
self.change_menu.grid(row=0, column=0)
self.exit_button.grid(row=1, column=1)
def check_value(self, event):
if event == "Plants":
print(event)
elif event == "Wood":
print(event)
elif event == "Mushrooms":
print(event)
elif event == "Animals":
print(event)
else:
print("No match")
if __name__ == "__main__":
root = tk.Tk()
Browser(root)
root.mainloop()
Thank you @Mike - SMT. It works just how I wanted and thank you for the critic!
– CodeMonster
Nov 16 '18 at 17:17
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%2f53324145%2fpassing-variables-between-functions-inside-class-for-optionmenu%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
First I want to say that not getting an error does not by default mean "good". There could be many reason for not getting an error and still have problems.
Now moving on to the code itself. You have a few problems.
Your method
func
is not doing anything useful here. Returning the event back to the menu item will result in nothing as it is being ignored by theOptionMenu
. So here you would get no error because it is ignored.Your
if/elif
statement will only run once and should fail with the error:NameError: name 'value' is not defined
. Due to value never being defined in thecreateUtility
method.Your use of
return
is not needed in any of these places and in fact does nothing useful.b = Browser(Tk()) mainloop()
Is not a good approach. You need to be more explicit on your tkinter instance.Small grievance but worth mentioning. Instead of doing
from tkinter import *
you should doimport tkinter as tk
and use thetk.
prefix where needed. This will help prevent accidental overtiring of imports form tkinter.
Change it to something like this:
if __name__ == "__main__":
root = Tk()
Browser(root)
root.mainloop()
Code:
import tkinter as tk
class Browser(tk.Frame):
def __init__(self, root):
super().__init__(root)
main_frame = tk.Frame(bg="")
display_frame = tk.Frame(width=50, height=50, bg="")
main_frame.grid(row=0, column=0)
display_frame.grid(row=1, column=0)
options = ["Plants", "Wood", "Mushrooms", "Animals"]
self.variable = tk.StringVar()
self.variable.set("Wood")
self.change_menu = tk.OptionMenu(main_frame, self.variable, *options, command=self.check_value)
self.exit_button = tk.Button(display_frame, text="Quit!", command=quit)
self.change_menu.grid(row=0, column=0)
self.exit_button.grid(row=1, column=1)
def check_value(self, event):
if event == "Plants":
print(event)
elif event == "Wood":
print(event)
elif event == "Mushrooms":
print(event)
elif event == "Animals":
print(event)
else:
print("No match")
if __name__ == "__main__":
root = tk.Tk()
Browser(root)
root.mainloop()
Thank you @Mike - SMT. It works just how I wanted and thank you for the critic!
– CodeMonster
Nov 16 '18 at 17:17
add a comment |
First I want to say that not getting an error does not by default mean "good". There could be many reason for not getting an error and still have problems.
Now moving on to the code itself. You have a few problems.
Your method
func
is not doing anything useful here. Returning the event back to the menu item will result in nothing as it is being ignored by theOptionMenu
. So here you would get no error because it is ignored.Your
if/elif
statement will only run once and should fail with the error:NameError: name 'value' is not defined
. Due to value never being defined in thecreateUtility
method.Your use of
return
is not needed in any of these places and in fact does nothing useful.b = Browser(Tk()) mainloop()
Is not a good approach. You need to be more explicit on your tkinter instance.Small grievance but worth mentioning. Instead of doing
from tkinter import *
you should doimport tkinter as tk
and use thetk.
prefix where needed. This will help prevent accidental overtiring of imports form tkinter.
Change it to something like this:
if __name__ == "__main__":
root = Tk()
Browser(root)
root.mainloop()
Code:
import tkinter as tk
class Browser(tk.Frame):
def __init__(self, root):
super().__init__(root)
main_frame = tk.Frame(bg="")
display_frame = tk.Frame(width=50, height=50, bg="")
main_frame.grid(row=0, column=0)
display_frame.grid(row=1, column=0)
options = ["Plants", "Wood", "Mushrooms", "Animals"]
self.variable = tk.StringVar()
self.variable.set("Wood")
self.change_menu = tk.OptionMenu(main_frame, self.variable, *options, command=self.check_value)
self.exit_button = tk.Button(display_frame, text="Quit!", command=quit)
self.change_menu.grid(row=0, column=0)
self.exit_button.grid(row=1, column=1)
def check_value(self, event):
if event == "Plants":
print(event)
elif event == "Wood":
print(event)
elif event == "Mushrooms":
print(event)
elif event == "Animals":
print(event)
else:
print("No match")
if __name__ == "__main__":
root = tk.Tk()
Browser(root)
root.mainloop()
Thank you @Mike - SMT. It works just how I wanted and thank you for the critic!
– CodeMonster
Nov 16 '18 at 17:17
add a comment |
First I want to say that not getting an error does not by default mean "good". There could be many reason for not getting an error and still have problems.
Now moving on to the code itself. You have a few problems.
Your method
func
is not doing anything useful here. Returning the event back to the menu item will result in nothing as it is being ignored by theOptionMenu
. So here you would get no error because it is ignored.Your
if/elif
statement will only run once and should fail with the error:NameError: name 'value' is not defined
. Due to value never being defined in thecreateUtility
method.Your use of
return
is not needed in any of these places and in fact does nothing useful.b = Browser(Tk()) mainloop()
Is not a good approach. You need to be more explicit on your tkinter instance.Small grievance but worth mentioning. Instead of doing
from tkinter import *
you should doimport tkinter as tk
and use thetk.
prefix where needed. This will help prevent accidental overtiring of imports form tkinter.
Change it to something like this:
if __name__ == "__main__":
root = Tk()
Browser(root)
root.mainloop()
Code:
import tkinter as tk
class Browser(tk.Frame):
def __init__(self, root):
super().__init__(root)
main_frame = tk.Frame(bg="")
display_frame = tk.Frame(width=50, height=50, bg="")
main_frame.grid(row=0, column=0)
display_frame.grid(row=1, column=0)
options = ["Plants", "Wood", "Mushrooms", "Animals"]
self.variable = tk.StringVar()
self.variable.set("Wood")
self.change_menu = tk.OptionMenu(main_frame, self.variable, *options, command=self.check_value)
self.exit_button = tk.Button(display_frame, text="Quit!", command=quit)
self.change_menu.grid(row=0, column=0)
self.exit_button.grid(row=1, column=1)
def check_value(self, event):
if event == "Plants":
print(event)
elif event == "Wood":
print(event)
elif event == "Mushrooms":
print(event)
elif event == "Animals":
print(event)
else:
print("No match")
if __name__ == "__main__":
root = tk.Tk()
Browser(root)
root.mainloop()
First I want to say that not getting an error does not by default mean "good". There could be many reason for not getting an error and still have problems.
Now moving on to the code itself. You have a few problems.
Your method
func
is not doing anything useful here. Returning the event back to the menu item will result in nothing as it is being ignored by theOptionMenu
. So here you would get no error because it is ignored.Your
if/elif
statement will only run once and should fail with the error:NameError: name 'value' is not defined
. Due to value never being defined in thecreateUtility
method.Your use of
return
is not needed in any of these places and in fact does nothing useful.b = Browser(Tk()) mainloop()
Is not a good approach. You need to be more explicit on your tkinter instance.Small grievance but worth mentioning. Instead of doing
from tkinter import *
you should doimport tkinter as tk
and use thetk.
prefix where needed. This will help prevent accidental overtiring of imports form tkinter.
Change it to something like this:
if __name__ == "__main__":
root = Tk()
Browser(root)
root.mainloop()
Code:
import tkinter as tk
class Browser(tk.Frame):
def __init__(self, root):
super().__init__(root)
main_frame = tk.Frame(bg="")
display_frame = tk.Frame(width=50, height=50, bg="")
main_frame.grid(row=0, column=0)
display_frame.grid(row=1, column=0)
options = ["Plants", "Wood", "Mushrooms", "Animals"]
self.variable = tk.StringVar()
self.variable.set("Wood")
self.change_menu = tk.OptionMenu(main_frame, self.variable, *options, command=self.check_value)
self.exit_button = tk.Button(display_frame, text="Quit!", command=quit)
self.change_menu.grid(row=0, column=0)
self.exit_button.grid(row=1, column=1)
def check_value(self, event):
if event == "Plants":
print(event)
elif event == "Wood":
print(event)
elif event == "Mushrooms":
print(event)
elif event == "Animals":
print(event)
else:
print("No match")
if __name__ == "__main__":
root = tk.Tk()
Browser(root)
root.mainloop()
answered Nov 15 '18 at 20:17
Mike - SMTMike - SMT
9,61621434
9,61621434
Thank you @Mike - SMT. It works just how I wanted and thank you for the critic!
– CodeMonster
Nov 16 '18 at 17:17
add a comment |
Thank you @Mike - SMT. It works just how I wanted and thank you for the critic!
– CodeMonster
Nov 16 '18 at 17:17
Thank you @Mike - SMT. It works just how I wanted and thank you for the critic!
– CodeMonster
Nov 16 '18 at 17:17
Thank you @Mike - SMT. It works just how I wanted and thank you for the critic!
– CodeMonster
Nov 16 '18 at 17:17
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%2f53324145%2fpassing-variables-between-functions-inside-class-for-optionmenu%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
I don't get errors, so that's good.... This is not always a good thing. At a glance I see some stuff I would change.
– Mike - SMT
Nov 15 '18 at 20:02
Yeah, I realised now.
– CodeMonster
Nov 16 '18 at 15:36