How to get scale value with button press tkinter? - python

I really did my best to find the solution on my own, but haven't. I want to have the value from a slider and then save it to a csv file (which is working fine), at the click of a button. Alas, I can't get the value of the tkinter.Scale during my button event. I wonder if it global variables might solve my problem, but I haven't gotten them to work. I'm particularly surprised because I can print a live stream of the scale's value as I change it, but can't save it in a useful way. If you could answer any of my befuddlement or let me know if my question is unclear or in anyway could be better, I'd greatly appreciate it. Here are some links to things that helped me get this far:
https://www.programiz.com/python-programming/global-local-nonlocal-variables
Tkinter - Get the name and value of scale/slider
Here is my attempt to print the final value 10 times:
from tkinter import *
root = Tk()
def scaleevent(v): #Live update of value printed to shell
print(v)
variable = v
def savevalue():
global variable #This is what I want to work, but doesn't
for i in range(10):
print(variable)
scale = Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
button = Button(text="button", command=savevalue).grid(column=1, row=0)
root.mainloop()
And here is my attempt to solve my problem using .get():
from tkinter import *
root = Tk()
def savevalue(): #print value 10 times. In final version I will save it instead
for i in range(10):
print(scale.get()) #I really want this to work, but it doesn't,
root.destroy #is it because .get is in a function?
scale = Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
button = Button(text="button", command=savevalue).grid(column=1, row=0)
root.mainloop()
(Python 3.5, Windows 10)
Edit:
This is the error I get from the first attempt using a global variable:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Me\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
File "C:\Users\Me\Documents\programing\tkinter scale question.py", line 15, in savevalue
print(variable)
NameError: name 'variable' is not defined
That's what happened when I run the first example of code, and similarly my actual project. Thanks Bryan Oakley!

You have to use global in scaleevent because you try to assing value to variable. Without global it assigns v to local variable and then it doesn't exists in savevalue
from tkinter import *
root = Tk()
def scaleevent(v):
global variable
print(v)
variable = v
def savevalue():
print(variable)
Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
Button(text="button", command=savevalue).grid(column=1, row=0)
root.mainloop()
As for second version you made mistake with var = Widget(...).grid()
It assigns None to var because grid()/pack()/place() returns None.
You have to do it in two lines:
var = Widget(...)
var.grid(...)
Code
from tkinter import *
root = Tk()
def savevalue():
print(scale.get())
root.destroy() # you forgot ()
scale = Scale(orient='vertical')
scale.grid(column=0,row=0)
button = Button(text="button", command=savevalue)
button.grid(column=1, row=0)
root.mainloop()

from tkinter import *
root = Tk()
variable=0 # only you forgot this
def scaleevent(v):
print(v)
global variable
variable=v
def savevalue():
print(variable)
Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
Button(text="button", command=savevalue).grid(column=1, row=0)
root.mainloop()

Related

how does the Entry widget in tkinter work?

I'm still a beginner with Tkinter and I'm not quite sure how the Entry widget work. I can't seem to get the value I enter I tried binding the root window to this function but I can't figure out why it's not working.
def get_value(event):
current_obj = root.focus_get()
if (current_obj in entries):
text = current_obj.get()
data.append(text)
You can use get to get the value from the entry.
First you define the entry like this:
e = tk.Entry()
e.pack()
Then you can have a static function which gets the value of the entry by calling entry.get()
def get_entry_value(entry)
entry.get()
Alternatively, if you have multiple entries in your app which are all contained in some iterable:
def get_entries(self, event=None):
data = list()
for e in self.entries:
data.append(e.get())
return data

Python Tkinter entry not loading content correct

i am facing a problem. I'm runnig this code.
import tkinter as tk
root = tk.Tk()
def check():
if len(e.get().split("a")) > 1:
print("contains a")
e = tk.Entry(frame1)
e.grid(row=4,column=1,columnspan=2,padx = (10,10), pady=(5,10), sticky="w e")
e.bind("<Key>",check)
when i type "a" to the entry I wont get nothing printed. I'll get the result by tiping a second character. I think that it happens because the function gets executed before the content has actualy changed. I tried to add a timer on the beginning of the function but it does nothing.
I want get the result by entering the first "a". What should I do?
I think that it happens because the function gets executed before the content has actualy changed.
You're right. If you want the callback to be able to see the character you just typed, you should create a StringVar and bind to that instead of binding to a "<Key>" event on the widget.
import tkinter as tk
frame1 = tk.Tk()
def check(*args):
if "a" in s.get():
print("contains a")
s = tk.StringVar()
e = tk.Entry(frame1, textvariable=s)
s.trace("w", check)
e.grid(row=4,column=1,columnspan=2,padx = (10,10), pady=(5,10), sticky="w e")
frame1.mainloop()

Python Tkinter Use Entry Field to Create MessageBox

I'm somewhat new to Tkinter with minor Python experience so I hope the answer's not too obvious, I've attempted to search for an answer but couldn’t find anything helpful. Essentially I'm trying to build a program where (as a placeholder test for now), if a user enters 1 in the entry field and hits submit, a window appears telling them they typed 1, otherwise they're told to type 1. If my understandings correct, this should work:
from Tkinter import *
#-----------------------------------------------------------
import tkMessageBox
root = Tk()
#-----------------------------------------------------------
root.title('Payroll System')
#-----------------------------------------------------------
def on_printtext(root):
global entryform
string = entryform.get()
if string == 1:
tkMessageBox.showinfo('You typed 1')
elif string != 1:
tkMessageBox.showinfo('Please type 1')
#-----------------------------------------------------------
entryform = Entry(root)
entryform.pack()
submit = Button(root, text="Submit", command=on_printtext)
submit.pack()
root.mainloop()
However when I try to run it and enter 1 in the entry form after hitting submit I get this:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in call
return self.func(*args)
TypeError: on_printtext() takes exactly 1 argument (0 given)
The issue is that tkinter is trying to call the function registered as the command for the button without any arguments, but your function has 1 argument - root without any default variable, and hence that is causing the issue you are having.
Also some other issues in your code -
Entry.get() returns a string, but you are trying to compare that against an integer , it would never be equal, so even if you enter 1 it would still show Please type 1 .
When you do - tkMessageBox.showinfo('You typed 1') - you are actually setting the title to You typed 1 , not the actual message. For the functions of tkMessageBox, the first argument is the title, and the second argument is the message. If you want that as the message, set it as themessage` using keyword argument. Example -
tkMessageBox.showinfo(message='You typed 1')
Example code that works -
from Tkinter import *
import tkMessageBox
root = Tk()
root.title('Payroll System')
def on_printtext():
global entryform
strng = entryform.get()
if strng == '1':
tkMessageBox.showinfo(message='You typed 1')
else:
tkMessageBox.showinfo(message='Please type 1')
entryform = Entry(root)
entryform.pack()
submit = Button(root, text="Submit", command=on_printtext)
submit.pack()
root.mainloop()
If you are using Python 3.x, the code above does not work since tkMessageBox has been changed to messagebox.
Here's the modified code:
from tkinter import * # modif 1 Tkinter with minus t !
import tkinter.messagebox # modif 2:tkMessageBox no longer valid
root = Tk()
root.title('Payroll System')
def on_printtext():
global entryform
strng = entryform.get()
if strng == '1':
tkinter.messagebox.showinfo(message='You typed 1') # modif 3
else:
tkinter.messagebox.showinfo(message='Please type 1') # modif 4
entryform = Entry(root)
entryform.pack()
submit = Button(root, text="Submit", command=on_printtext)
submit.pack()
root.mainloop()

Pausing from within a tkinter function

I'm trying to create a couple of functions which do things in a sequential order. First they need to open a new window and display a label, then they need to wait for some seconds, then they need to call another function. However, I'm struggling to get the functions to wait, all the methods I've tried (.after, .sleep, .wait_visibility) seem to be ignored and it just skips to the next function call without pausing.
Here's what I have (sorry if it's messy, I'm new to python):
from tkinter import *
import time
root =Tk()
root.geometry('600x600')
def scale_screen(event = None):
global s_screen
s_screen = Toplevel(root)
s_screen.title('Residual Inhibition Tester')
s_screen.geometry('600x600')
s_screen.transient(root)
s_screen.bind('<Return>', sel)
global var
var = IntVar()
scale = Scale(s_screen, variable = var, orient = HORIZONTAL, length = 1000)
scale.focus_set()
scale.pack(anchor=CENTER)
button = Button(s_screen, text="Select", command=sel)
button.pack(anchor=CENTER)
def sel(event = None):
label = Label(s_screen)
selection = "Value = " + str(var.get())
label.config(text = selection)
interval_screen()
def interval_screen():
global i_screen
i_screen = Toplevel(root)
i_screen.geometry('600x600')
i_screen.transient(root)
i_label = Label(i_screen, text = "Please Wait")
i_label.pack(anchor = CENTER)
s_screen.destroy()
i_screen.after(3000, masker_screen)
#time.sleep(3)
#i_screen.after(300,i_label.configure(text="Playing New Masker Noise"))
#root.wait_visibility(window = i_screen)
def masker_screen():
global m_screen
m_screen = Toplevel(root)
m_screen.geometry('600x600')
m_screen.transient(root)
m_label = Label(m_screen, text = "Playing New Masker Noise").pack(anchor = CENTER)
m_screen.after(3000, lambda: scale_screen(event = None))
i_screen.destroy()
b1 = Button(root, command = scale_screen).pack(anchor=CENTER)
root.bind('<Return>', scale_screen)
root.mainloop()
In this example, the program will run but just skip the interval_screen entirely and just do the masker_screen. I'm also not averse to just using one screen and using the .configure methods to change the label text if that's easier.
Thanks!
Without seeing all the ways you tried it, it's impossible to know what you did wrong. In general you should never call time.sleep and you should never call after with just a single argument. Also, when you use after with two arguments, the second argument must be a reference to a function.
The proper way to do this is to have your first function call your second function via after:
def interval_screen():
...
i_screen.after(3000, maker_screen)
def masker_screen():
...
m_screen.after(3000, lambda: scale_screen(event = None))
Note that in your updated question you're using after incorrectly:
m_screen.after(3000, scale_screen(event = None))
You're calling the function scale_screen(...) immediately, and giving the result of that to the after function. If you need to pass arguments to your function you must create another function that does not require arguments. The simplest way to do this is with lambda, though you can also use functools.partial or you can create your own function.

Python Execution Order with GUI

I'm having some issues with the following code. This is the first time that I'm working with a GUI and it's been a while since I've worked with python as well. When I try to execute the solfield function with the button, it yields no output.
from Tkinter import *
import math
master = Tk()
n = float()
I = float()
def solfield():
pass
label_coils = Label(text='Number of Coils Per Meter', textvariable=n)
label_coils.grid()
coils = Entry(master)
coils.grid()
label_current = Label(text='Current in Amps', textvariable=I)
label_current.grid()
current = Entry(master)
current.grid()
calculate_button = Button(text='Calculate', command=solfield())
calculate_button.grid()
label_bfield = Label(text='B Field in +z Direction')
label_bfield.grid()
label_result = Label(text='solfield')
label_result.grid()
master.title('Coil Gun Simulation')
master.mainloop()
def solfield():
mu0 = math.pi*4e-7
solfield = mu0*n*I
print solfield
Any other tips would be appreciated as well, as there will eventually be much more coding for me to do.
This has been solved. If anyone is interested, here is the code after several fixes were made:
from Tkinter import *
import math
master = Tk()
label_coils = Label(text='Number of Coils Per Meter')
label_coils.grid()
coils = Entry(master)
coils.grid()
label_current = Label(text='Current in Amps')
label_current.grid()
current = Entry(master)
current.grid()
def solfield():
mu0 = math.pi*4e-7
n = float(coils.get())
I = float(current.get())
fieldmag = mu0*n*I
print fieldmag
calculate_button = Button(text='Calculate', command=solfield)
calculate_button.grid()
label_bfield = Label(text='B Field in +z Direction')
label_bfield.grid()
label_result = Label(text='solfield')
label_result.grid()
master.title('Coil Gun Simulation')
master.mainloop()
The problem is here:
calculate_button = Button(text='Calculate', command=solfield())
To pass the function solfield itself as the command, just use its name:
calculate_button = Button(text='Calculate', command=solfield)
What you're doing is calling the function, and then passing the return value of that function as the command.
Since you defined solfield above as do-nothing function, that return value is None, so you're telling calculate_button that its command=None, and it's properly doing nothing.
Meanwhile, as SethMMorton pointed out (but then deleted):
You have two functions named solfield, and you are naming a variable solfield in one of your solfield functions. Remove the empty function (the one with pass), and using a different variable name in the remaining function.
This isn't causing your actual problem, but it's certainly adding to the confusion that makes it harder for you to find the problem. (For example, if you hadn't included the excess empty definition of solfield at all, you would have gotten a NameError in the incorrect line, which would have made things easier to debug.)
Putting it all together, what you should do is:
Get rid of the empty (pass-only) definition of solfield.
Move the real implementation of solfield up above the point where you build the GUI.
Don't name a local variable solfield within the function.
Pass just solfield, not solfield() as the command for calculate_button.

Categories

Resources