Okay, so I am trying to make a menu system using Tkinter and am trying to save the string value of the drop down menu to a class variable. I have code to handle that part, but the problem comes with getting that string value to the function I have written. I know that it isn't my function that is the problem as I am using the print function for my example below.
import tkinter as tk
from enum import Enum
class CustomEnum(Enum):
Option1 = 'Option1'
Option2 = 'Option2'
class window():
def __init__(self, root):
self.value = CustomEnum.Option1
test = tk.StringVar()
test.set(self.value.value)
tk.OptionMenu(root, test, *[e.value for e in CustomEnum], command = lambda
content = test.get() : print(content)).pack()
tk.Button(root, text="Save",
command = lambda content = test.get() : print(content)).pack()
root = tk.Tk()
test = window(root)
root.mainloop()
If you run this code, it'll constantly print "Option 1" despite what option you chose or if you add or remove elements (aside from removing Option 1).
The problem lies in this line
tk.Button(root, text="Save",
command = lambda content = test.get() : print(content)).pack()
You are assigning content the value of test.get() which was at that instant (Option1) and it continues to be so unchanged.
Since you want the current value of test.get(), you would have to do this
command = lambda: print(test.get())).pack()
Also, I believe you have misspelt customEnum instead of CustomEnum.
Related
Eventually I want to use the values in the comboboxes as parameters in other functions, but I think if I can just get them to print for now, that will be enough to build off of. Here's what I have so far.
import tkinter as tk
from tkinter import ttk
import time
def ok():
betType = betTypeVar.get()
season = seasonVar.get()
print(betType, season)
def CreateSimPreviousSeasonWindow():
prevSeasonWindow = tk.Tk()
#============= Bet Type Input =============#
betTypeVar = tk.StringVar()
betTypeLabel = tk.Label(prevSeasonWindow, text="Bet type:").grid(row=0,column=0)
betTypeChosen = ttk.Combobox(prevSeasonWindow, values=['Moneyline','Total'])
betTypeChosen.grid(row=0, column=1)
seasonVar = tk.StringVar()
seasonLabel = tk.Label(prevSeasonWindow, text='Season:').grid(row=1, column=0)
seasonChosen = ttk.Combobox(prevSeasonWindow, values=['2018', '2017'])
seasonChosen.grid(row=1,column=1)
button = tk.Button(prevSeasonWindow, text='OK', command=ok)
button.grid(row=2,column=0)
prevSeasonWindow.mainloop()
This gives me
File "C:[directory...]", line 6, in ok
betType = betTypeVar.get()
NameError: name 'betTypeVar' is not defined
To me it looks pretty obvious that this error is because ok() doesn't have any parameters passed to it, so it has no idea what 'betTypeVar' is, but all the tutorials I've read do it this way, so I'm missing something. If I try actually passing ok() the arguments, it still doesn't work.
There are two things to fix in your code. First let's focus on CreateSimPreviousSeasonWindow:
betTypeVar = tk.StringVar()
seasonVar = tk.StringVar()
You defined two StringVar but you actually never used it or linked them to your combobox object. The correct way is to set them as a textvaraible:
betTypeChosen = ttk.Combobox(prevSeasonWindow, textvariable=betTypeVar, values=['Moneyline','Total'])
seasonChosen = ttk.Combobox(prevSeasonWindow, textvariable=seasonVar, values=['2018', '2017'])
Next, NameError: name 'betTypeVar' is not defined is due to your variables being local variables. You are trying to access the same variable across different functions. To pass them around, you need to declare global:
def ok():
global betTypeVar, seasonVar
betType = betTypeVar.get()
season = seasonVar.get()
print(betType, season)
def CreateSimPreviousSeasonWindow():
global betTypeVar, seasonVar
...
Also I want to point out that if you just want to retrieve the values of the combobox, you don't really need to create two StringVar. Just combobox.get() already works good enough.
import tkinter as tk
from tkinter import ttk
import time
def ok():
global betTypeChosen, seasonChosen
print (betTypeChosen.get(), seasonChosen.get())
def CreateSimPreviousSeasonWindow():
global betTypeChosen,seasonChosen
prevSeasonWindow = tk.Tk()
#============= Bet Type Input =============#
betTypeLabel = tk.Label(prevSeasonWindow, text="Bet type:").grid(row=0,column=0)
betTypeChosen = ttk.Combobox(prevSeasonWindow,values=['Moneyline','Total'])
betTypeChosen.grid(row=0, column=1)
seasonLabel = tk.Label(prevSeasonWindow, text='Season:').grid(row=1, column=0)
seasonChosen = ttk.Combobox(prevSeasonWindow, values=['2018', '2017'])
seasonChosen.grid(row=1,column=1)
button = tk.Button(prevSeasonWindow, text='OK', command=ok)
button.grid(row=2,column=0)
prevSeasonWindow.mainloop()
CreateSimPreviousSeasonWindow()
I would like to sort numbers when I clicked sort numbers Radiobutton. I already achieve this by calling a function when the Radiobutton is clicked. however, i couldn't sort numbers without calling a function.
this is my code
R1=Radiobutton(root,text="Sort Student Numbers",value=1)
R1.pack(anchor=W)
R2=Radiobutton(root,text="Sort Student Names",value=2)
R2.pack(anchor=W)
with open("student.json", "r"") as f:
data = json.load(f)
for d in data["student"]:
if value == 1:
data["student"].sort(key = lambda d: d["Numbers"])
elif value == 2:
data["student"].sort(key = lambda d: d["Names"])
label_1 = Label(frame , text="Name: %s" %(d["Names"]))
label_1.pack()
label_2 = Label(frame , text="Student Numbers: %d" %(d["Numbers"]))
label_2.pack()
if I say for example R1=Radiobutton(root,text="Sort Student Numbers",value=1, command = sorted_numbers(1)) everything works fine but the reason I don't want to use function calling is I would have to create 3 functions to achieve what I want. thanks
One way to solve this problem is to tie these radio buttons to a shared tkinter variable instance. When a radio button is selected, the value of the variable will be set to the value of the radio button, and then you can use that value in your code.
I haven't had time to test this code, but I have copied your code and modified it in a way that should work. This code assumes that you are importing everything from tkinter, using the line from tkinter import *; otherwise, you will need to do something like from tkinter import IntVar. There are several types of tkinter variable subclasses (IntVar, BooleanVar, etc.), and each has the methods get and set, which behave exactly as you'd expect (as demonstrated below).
# This is the variable that will store the value of the currently selected radio button
sort_value = IntVar()
# For each radio button, assign sort_value to the keyword parameter "variable"
R1=Radiobutton(root,text="Sort Student Numbers",variable=sort_value,value=1)
R1.pack(anchor=W)
R2=Radiobutton(root,text="Sort Student Names",variable=sort_value,value=2)
R2.pack(anchor=W)
with open("student.json", "r") as f:
data = json.load(f)
for d in data["student"]:
# sort_value is an IntVar, so sort_value.get returns a Python int
if sort_value.get() == 1:
data["student"].sort(key = lambda d: d["Numbers"])
elif sort_value.get() == 2:
data["student"].sort(key = lambda d: d["Names"])
label_1 = Label(frame , text="Name: %s" %(d["Names"]))
label_1.pack()
label_2 = Label(frame , text="Student Numbers: %d" %(d["Numbers"]))
label_2.pack()
Edit: Like Nae pointed out in the comments, you can also initialize the variable to a default value like this:
sort_value = IntVar(value=1)
Otherwise, its default value will be 0. I believe that by setting it to 1, this will also cause the radio button whose value is 1 to be selected by default.
I hope this helps.
I'm new in python programming and I'm having some issues in developing a specific part of my GUI with Tkinter.
What I'm trying to do is, a space where the user could enter (type) his math equation and the software make the calculation with the variables previously calculated.
I've found a lot of calculators for Tkinter, but none of then is what I'm looking for. And I don't have much experience with classes definitions.
I made this simple layout to explain better what I want to do:
import tkinter as tk
root = tk.Tk()
Iflabel = tk.Label(root, text = "If...")
Iflabel.pack()
IfEntry = tk.Entry(root)
IfEntry.pack()
thenlabel = tk.Label(root, text = "Then...")
thenEntry = tk.Entry(root)
thenlabel.pack()
thenEntry.pack()
elselabel = tk.Label(root, text = "else..")
elseEntry = tk.Entry(root)
elselabel.pack()
elseEntry.pack()
applybutton = tk.Button(root, text = "Calculate")
applybutton.pack()
root.mainloop()
This simple code for Python 3 have 3 Entry spaces
1st) If...
2nd Then...
3rd) Else...
So, the user will enter with his conditional expression and the software will do the job. In my mind, another important thing is if the user left the "if" space in blank, he will just type his expression inside "Then..." Entry and press the button "calculate" or build all expression with the statements.
If someone could give some ideas about how and what to do....
(without classes, if it is possible)
I'l give some situations for exemplification
1st using statements:
var = the variable previously calculated and stored in the script
out = output
if var >= 10
then out = 4
else out = 2
2nd Without using statement the user will type in "Then" Entry the expression that he want to calculate and that would be:
Then: Out = (((var)**2) +(2*var))**(1/2)
Again, it's just for exemplification...I don't need this specific layout. If anyone has an idea how to construct it better, is welcome.
Thanks all.
Here is a simple version of what you are trying to do.
We need to use the eval built in function to evaluate the math of a string.
We should also write our code with some error handling as there is a very good change a user will type a formula wrong and the eval statement will fail.
For more information on eval and exec take a look at this post here. I think it does a good job of explaining the two.
Here is what it would look like:
import tkinter as tk
root = tk.Tk()
math_label = tk.Label(root, text = "Type formula and press the Calculate button.")
math_label.pack()
math_entry = tk.Entry(root)
math_entry.pack()
result_label = tk.Label(root, text = "Results: ")
result_label.pack(side = "bottom")
def perform_calc():
global result_label
try:
result = eval(math_entry.get())
result_label.config(text = "Results: {}".format(result))
except:
result_label.config(text = "Bad formula, try again.")
applybutton = tk.Button(root, text = "Calculate", command = perform_calc)
applybutton.pack()
root.mainloop()
The first answer gets at the right thought, but it can also be matched a little more explicitly to the example you gave, in case you want to take this a little further.
Basically you want to use the eval statement to test your conditional, and then use the exec statement to run your python code blocks. You have to pass in the globals() argument in order to make sure your exec functions modify the correct variables in this case
See below:
import tkinter as tk
from tkinter import messagebox
var = 10
out = 0
def calculate():
global out
try:
if eval(IfEntry.get()):
exec(thenEntry.get(), globals())
else:
exec(elseEntry.get(), globals())
messagebox.showinfo(title="Calculation", message="out: " + str(out))
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
msg = traceback.format_exception(exc_type, exc_value, exc_traceback)
messagebox.showinfo("Bad Entry", message=msg)
root = tk.Tk()
Iflabel = tk.Label(root, text = "If...")
Iflabel.pack()
IfEntry = tk.Entry(root)
IfEntry.insert(0, "var >= 10")
IfEntry.pack()
thenlabel = tk.Label(root, text = "Then...")
thenEntry = tk.Entry(root)
thenlabel.pack()
thenEntry.insert(0, "out = 4")
thenEntry.pack()
elselabel = tk.Label(root, text = "else..")
elseEntry = tk.Entry(root)
elselabel.pack()
elseEntry.insert(0, "out = 2")
elseEntry.pack()
applybutton = tk.Button(root, command=calculate, text = "Calculate")
applybutton.pack()
applybutton.focus_displayof
root.mainloop()
I've written an app that takes some data from the user, queries the API of a website, and returns and processes the data given by the API. I'm trying to allow the user greater control of what happens next by creating checkboxes for each item retrieved.
Because the number of items retrieved will vary on each use, I'm putting the checkboxes and the IntVars for if they're checked or not into lists. However, when I try to set the IntVars to 1 (so they all start out checked) I get a TypeError telling me it wants the IntVar instance as the first arg, but that doesn't make any sense to me as I'm trying to call a method for the IntVar in the first place.
This is a much simplified version of my code that generates an identical error:
import Tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.test_names = ["Box 1", "Box 2", "Box 3", "Box 4"]
self.boxes = []
self.box_vars = []
self.box_num = 0
btn_test1 = tk.Button(self, text="Test 1", width = 11, command = self.test1)
btn_test1.grid()
def test1(self):
for name in self.test_names:
self.box_vars.append(tk.IntVar)
self.boxes.append(tk.Checkbutton(self, text = name, variable = self.box_vars[self.box_num]))
self.box_vars[self.box_num].set(1)
self.boxes[self.box_num].grid(sticky = tk.W)
self.box_num += 1
root = tk.Tk()
app = Application(master=root)
app.mainloop()
And the error message when pushing the button:
TypeError: unbound method set() must be called with IntVar instance as first
argument (got int instance instead)
What am I doing wrong?
Classic problem. You add the type IntVar instead of adding an object of type IntVar. Add parentheses like this:
self.box_vars.append(tk.IntVar())
Yes you forgot the brackets. The result will be and not a tkinter intvar instance.
I'm working on a GUI for a project in school. All the buttons that I have in my GUI are bound with functions that I have created. These functions call for already predefined functions. For some of the predefined functions, I need one or two arguments and I have solved that with entries. I type in the arguments in the right entries that are connected to the specific button and when I press the button, the function will run with the corresponding arguments.
The thing I want to do is to in some way when I press a button, the function should be saved to a list instead of being executed right away. And when I push the "run" button(a new button that I will create) everything in my list will be executed. I have been thinking about using a list box but I don't know exactly how they work or if its even possible to run a list box that contains a number of functions. Does someone have any ideas or solutions for me? Can I use the list box for this or is there something else that is better to use?
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.entry1 = IntVar()
self.entry2 = IntVar()
def do_something():
value1 = self.entry1.get()
value2 = self.entry2.get()
self.listbox.insert(END, "predefined_function(value1, value2)")
def run_listbox_contents():
pass
self.button = Button(frame, text="Move", command=lambda: do_something())
self.button.pack(side=TOP)
self.entry1.set("value1")
self.entry = Entry(frame, textvariable=self.entry1)
self.entry.pack(side=TOP)
self.entry2.set("value2")
self.entry = Entry(frame, textvariable=self.entry2)
self.entry.pack(side=TOP)
self.listbox = Listbox(master)
self.listbox.pack(side=TOP)
root = Tk()
app = App(root)
root.title("Mindstorms GUI")
root.geometry("800x1200")
root.mainloop()
root.destroy()
Just use a standard list.
something like this
def hest(txt):
print "hest: " +txt
def horse(txt):
print "horse: " + txt
funcList = []
funcList.append(hest)
funcList.append(horse)
for x in funcList:
x("Wow")
This outputs
hest: Wow
horse: Wow
Was this what you wanted?
If I were you, I wouldn't want to save functions to a list. I would suggest another solution for you.
I suppose you have heard of the principle of MVC (Model-View-Controller). In your case, the list box is a part of view, and the process that saves functions and then calls them at once is a part of controller. Separate them.
You might want to save and display any string in the list box to let the users know that the corresponding functions have been enlisted and ready to run. For example, save a string "Function1 aug1 aug2 aug3" or "Funtion2 aug1 aug2" or whatever you like as a handle of the corresponding function.
And for the controller part, write a function (let's say conductor()). It reads the handle strings from the list, parses them and calls the corresponding functions. Where you want to run the enlisted functions, there you just call conductor().
Update:
Due to your comment I understand that you are pretty new to program. Let me show you how to write a simplest parser with your given variable names.
def run_listbox():
to_do_list = #get the list of strings
for handle_string in to_do_list:
#Let's say you got
#handle_string = "Predfined_function1 value1 value2"
#by here
handle = handle_string.split(" ")
#Split the string by space, so you got
#handle = ["Predfined_function1", "value1", "value2"]
#by here
if handle[0] == "Predfined_function1":
Predfined_function1(handle[1], handle[2]) #Call Predfined_function1(value1, value2)
elif handle[0] == "Predfined_function2":
Predfined_function2(handle[1], handle[2])
#elif ...
#...
#elif ...
#...
#elif ...
#...
This is not a perfect parser, but I hope it could let you know what does a parser look like.