Tkinker spinbox get - python

I'd like to print output value from spinbox, after clicking enter. I don't know why it is printing always value "from_" (so 1). What is the problem? Thank you.
import tkinter as tk
gui = tk.Tk()
gui.geometry("300x390")
gui.title("Test")
var = tk.IntVar()
tk.Spinbox(
gui,
textvariable=var,
from_=1,
to=10).pack()
a = var.get()
def print_test():
print(a)
enter = tk.Button(gui, text="Enter", command=print_test).pack(pady=15)
gui.mainloop()

Related

can not get the variable output in tkinter checkbutton

I am new to tkinter and learning to use the widget "CheckButton".
I have encountered a problem, whatever I select, the code prints 0.
Wanted:
print 1 if selected
print 0 if not selected
MWE
import tkinter as tk
win = tk.Tk()
win.title('My Window')
win.geometry('100x100')
l = tk.Label(win, width=20, text='MultipleReplace')
l.pack()
var_cb1 = tk.IntVar()
var_cb1.set(0)
cb1 = tk.Checkbutton(win, text='Yes',variable=var_cb1,onvalue=1,offvalue=0)
cb1.pack(side=tk.TOP)
var_cb1_val = var_cb1.get()
print('The selected value is : ', var_cb1_val)
win.mainloop()
You're calling get on var_cb1 pretty much immediately after setting it to 0, and then printing that value. You're not waiting for the checkbutton to change states and then printing the value. Define a function that gets called when the CheckButton changes state, then get the value there
import tkinter as tk
def on_update(var):
print('The selected value is: ', var.get()) # get the new value
win = tk.Tk()
win.title('My Window')
win.geometry('100x100')
l = tk.Label(win, width=20, text='MultipleReplace')
l.pack()
var_cb1 = tk.IntVar()
var_cb1.set(0)
cb1 = tk.Checkbutton(
win,
text='Yes',
variable=var_cb1,
onvalue=1,
offvalue=0,
# call 'on_update', using a lambda to pass in the 'var_cb1' variable
command=lambda var=var_cb1: on_update(var)
)
cb1.pack(side=tk.TOP)
win.mainloop()

simple Tkinter program - window vanishes after clicking button - maintop problem?

Coders,
I guess I have a newbie question: my windows disappears when I click on a button. If I put in root.mainloop() as last line in the buttonClicked-function, then the program is fine - but it looks not right...what is wrong here?
import tkinter as tk
def buttonClicked(event):
print(tf1.get())
tf1Content.set("button clicked")
# root.mainloop() ... would work
root = tk.Tk()
frame = tk.Frame(root, relief="ridge", borderwidth=2)
frame.pack(fill="both",expand=1)
label = tk.Label(frame, text="Input:")
label.pack(expand=1)
tf1Content = tk.StringVar()
tf1 = tk.Entry(frame, text="input here", textvariable=tf1Content)
tf1.pack(expand=1)
bOk = tk.Button(frame,text="OK",command=root.destroy)
bOk.bind("<Button-1>", buttonClicked)
bOk.widget = "bOK"
bOk.pack(side="bottom")
tf1.focus()
root.mainloop()
It turns out that you just copied this line:
bOk = tk.Button(frame,text="OK",command=root.destroy)
which binds a call to root.destroy() to the button press.
The fix is to just remove the command parameter:
bOk = tk.Button(frame,text="OK")

Returning values from entry's when pressing a button using Tkinter

I am writing a program that requires two values (from 2 entries) to be called when a button is pressed. I simplified to code to try to isolate the problem. For some reason the program is not behaving how I want it to. When the button is pressed, the output is "A=" and then if i click the button a second time I get ""A=entry1 B=entry2 A=". I have been trying to figure out the problem for hours now, please help.
import tkinter as tk
def button_function():
A = entry1.get()
print('A=', A)
B= entry2.get()
print('B=', B)
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
entry1 = tk.Entry(root)
entry1.place(relwidth=0.5, relheight=0.5)
entry2 = tk.Entry(root)
entry2.place(rely=0.5, relwidth=0.5, relheight=0.5)
button = tk.Button(root, text = "confirm", command= button_function)
button.place(relx=0.5, relwidth=0.5, relheight=1)
root.mainloop()
You just need to change command=button_function() to command=button_function, then it will work perfectly!

Tkinter (Python) output random numbers to GUI

Noob Alert!!!
Hello, I just started my journey with through python a couple of days ago, so my question will most likely be extremely simple to answer. Basically I have a random number gen. from 1 to 10, this is activated when the "test button is pressed on the windows that pops up. As you can see from the image below the random number appears in the output console on the bottom of the screen, in this case it was a 9. So here's the question, How can I make the random number on the GUI? so when the button is pressed a random number appears on the same window as the button.
https://i.stack.imgur.com/hWd3i.png
Any help is appreciated!
from tkinter import *
root = Tk()
root.geometry("300x300")
root.title("test it is")
root.grid()
def randnum(event):
import random
value =random.randint(1,10)
print(value)
button_1 = Button(root, text="test")
button_1.bind("<Button-1>",randnum)
button_1.pack()
root.mainloop()
from tkinter import *
root = Tk()
root.geometry("300x300")
root.title("test it is")
root.grid()
def randnum(event):
import random
value =random.randint(1,10)
print(value)
updateDisplay(value)
def updateDisplay(myString):
displayVariable.set(myString)
button_1 = Button(root, text="test")
button_1.bind("<Button-1>",randnum)
button_1.pack()
displayVariable = StringVar()
displayLabel = Label(root, textvariable=displayVariable)
displayLabel.pack()
root.mainloop()
Here is what it looks like.You have to create a Label with a Button, whose value will get updated when you click on button.
import tkinter as tk
from random import randint
win = tk.Tk()
def test_button_click():
label_val.set(randint(1, 10))
my_button = tk.Button(win, text='Test Button',
command=test_button_click)
my_button.grid(column=0, row=0)
label_val = tk.IntVar()
my_Label = tk.Label(win, textvariable=label_val)
my_Label.grid(column=1, row=0)
win.mainloop()
This will achieve what you are requesting -- create a tk window, add a button and label, use the callback test_button_click to set the labels int var when the button is clicked.

How do I get the Entry's value in tkinter?

I'm trying to use Tkinter's Entry widget. I can't get it to do something very basic: return the entered value. Does anyone have any idea why such a simple script would not return anything? I've tried tons of combinations and looked at different ideas.
This script runs but does not print the entry:
from Tkinter import *
root = Tk()
E1 = Entry(root)
E1.pack()
entry = E1.get()
root.mainloop()
print "Entered text:", entry
Seems so simple.
Edit
In case anyone else comes across this problem and doesn't understand, here is what ended up working for me. I added a button to the entry window. The button's command closes the window and does the get() function:
from Tkinter import *
def close_window():
global entry
entry = E.get()
root.destroy()
root = Tk()
E = tk.Entry(root)
E.pack(anchor = CENTER)
B = Button(root, text = "OK", command = close_window)
B.pack(anchor = S)
root.mainloop()
And that returned the desired value.
Your first problem is that the call to get in entry = E1.get() happens even before your program starts, so clearly entry will point to some empty string.
Your eventual second problem is that the text would anyhow be printed only after the mainloop finishes, i.e. you close the tkinter application.
If you want to print the contents of your Entry widget while your program is running, you need to schedule a callback. For example, you can listen to the pressing of the <Return> key as follows
import Tkinter as tk
def on_change(e):
print e.widget.get()
root = tk.Tk()
e = tk.Entry(root)
e.pack()
# Calling on_change when you press the return key
e.bind("<Return>", on_change)
root.mainloop()
from tkinter import *
import tkinter as tk
root =tk.Tk()
mystring =tk.StringVar(root)
def getvalue():
print(mystring.get())
e1 = Entry(root,textvariable = mystring,width=100,fg="blue",bd=3,selectbackground='violet').pack()
button1 = tk.Button(root,
text='Submit',
fg='White',
bg= 'dark green',height = 1, width = 10,command=getvalue).pack()
root.mainloop()

Categories