Essentially, I'm using Tkinter in python so that 1 is added to a variable when a button is pushed. I have another variable that is equal to my first variable times 10. When I hit the button that adds one to my original variable, the second variable (x*10) is not doubled. Take a look:
def add1():
var1.set(var1.get() + 1)
def pringvar3()
print(var3.get())
from tkinter import *
main = Tk()
button = Button(main, text="Add 1", command=add1)
button.pack()
button2 = Button(main, text="Print var3", command=printvar3)
button2.pack()
var1 = IntVar()
var1.set(1)
var2 = IntVar()
var2.set(10)
var3 = IntVar()
var3.set(var2.get() * var1.get())
What's wrong with that code? When I click the buttons, first 1 then 2, it still prints the variable as 10.
You have a bunch of spelling errors and missing colons, but more importantly you don't change the value in the function callback. Remember, you only set the value once, you never actually reset it.
def printvar3():
global var3
var3.set(var2.get() * var1.get())
print(var3.get())
tkinter IntVar objects don't update others that may have used their value at some point, so changing var1 won't automatically update var3 -- you have to make it happen explicitly.
Here's a simple demonstration based on your code that has an extra button added that does this when clicked:
def add1():
var1.set(var1.get() + 1)
def updatevar3():
var3.set(var2.get() * var1.get()) # recalulate value
def printvar3():
print(var3.get())
from tkinter import *
main = Tk()
button = Button(main, text="Add 1", command=add1)
button.pack()
button2 = Button(main, text="Update var3", command=updatevar3)
button2.pack()
button3 = Button(main, text="Print var3", command=printvar3)
button3.pack()
var1 = IntVar()
var1.set(1)
var2 = IntVar()
var2.set(10)
var3 = IntVar()
var3.set(var2.get() * var1.get()) # set initial value
main.mainloop()
print('done')
Related
I'm trying to create a function in tkinter where I can print out what the user writes in a Entry box. I'm able to print out ask_an_entry_get, but when I try to print what_is_answer_entry_get
, I get nothing my empty spaces.
Please find out the problem here. Also I'm using the Entry widget, along with the get() function, to get input from the user.
def answer_quizmaker_score():
print(ask_an_entry_get)
print(what_is_answer_entry_get)
I made a lot of global variables so I could use them all around my code.
global what_is_answer_entry
what_is_answer_entry = Entry(root4)
what_is_answer_entry.pack()
I then used the get() function to retrieve what the user typed.
global what_is_answer_entry_get
what_is_answer_entry_get = what_is_answer_entry.get()
This is the exact process I did for both ask_an_entry_get and what_is_answer_entry_get. However for some reason only ask_an_entry_get is printed, while what_is_answer_entry_get is printing nothing in the console.
from tkinter import *
root = Tk()
root.geometry("500x500")
txt1 = StringVar()
txt2 = StringVar()
def txt_printer():
print(txt1.get())
print(txt2.get())
x = Entry(root, textvariable=txt1, width=20)
x.place(x=0, y=0)
y = Entry(root, textvariable=txt2, width=20)
y.place(x=0, y=50)
btn_print = Button(root, text="print", command=txt_printer)
btn_print.place(x=0, y=100)
# Or if you want to show the txt on window then:
def txt_on_window():
lb1 = Label(root, text=txt1.get())
lb1.place(x=0, y=200)
lb2 = Label(root, text=txt2.get())
lb2.place(x=0, y=235)
btn_print_on_window = Button(root, text="print on screen", command=txt_on_window)
btn_print_on_window.place(x=0, y=150)
root.mainloop()
I have a check button in my program. when I click the check button, the var value is always 0. Shouldn't the value be 1 when clicked. It works in a small program that i made but when I try to get a check button value in my big program, the value is always 0 no matter how many times I click the checkbutton. The following code is the small example program that I wrote which works just fine but when I try to get the var value in my bigger program, the value is always 0.
from tkinter import *
root = Tk()
root.geometry("400x400")
def check():
global var2
var2 = IntVar()
c=Checkbutton(root, text="click me", variable=var2, command=show)
c.pack()
myButton=Button(root, text="show selection", command=show).pack()
def show():
myLabel = Label(root, text=var2.get()).pack()
check()
root.mainloop()
Well, I figured it out. Well, thanks to Google. When opening up a new window instead of calling Tk() I changed it to Toplevel() and it finally worked. Thanks for all of your input.
You needed to use BooleanVar(). Hope it helps.
from tkinter import *
root = Tk()
root.geometry("400x400")
def check():
global var2
var2 = BooleanVar()
c=Checkbutton(root, text="click me", variable=var2, command=show)
c.pack()
myButton=Button(root, text="show selection", command=show).pack()
def show():
myLabel = Label(root, text=int(var2.get())).pack()
# commented out the lower label, it returned False instead of one.
# It's always a good idea to force variables to be what they should be.
# myLabel = Label(root, text=var2.get()).pack()
check()
root.mainloop()
I'm a having a problem that I can't check if the checkbutton in tkinter is checked or not (I always get False).
I'm sure I'm missing something but I've tried for so long and I don't know what I'm doing wrong.
Bellow is the minimum code I could write that shows the problem:
There are two files:
The main file (that you run) is called "main_GUI.py" and here's the code:
from tkinter import *
import sub_GUI
root = Tk()
button1 = Button(root, bg='white', text="Click me", font=("Helvetica",12),
height=3, width=25, command=sub_GUI.create_new_window)
button1.grid(column=1, row=1)
root.mainloop()
The second file is called "sub_GUI.py" and here's the code:
from tkinter import *
var1 = None
sub_root = None
def create_widgets():
global sub_root
global var1
var1 = BooleanVar()
Checkbutton(sub_root,
text="A",
variable=var1,
command=do_something
).grid(row=2, column=0, sticky=W)
def do_something():
global var1
is_current_joint_checked = var1.get()
if is_current_joint_checked:
print('True')
else:
print('False')
def create_new_window():
global sub_root
sub_root = Tk()
sub_root.title("Movie Chooser")
create_widgets()
The problem is that in the window that appears with the letter 'A' (after you clicked the button), every time I check/uncheck 'A' it prints False (never prints True).
Can anyone tell me what's the problem?
Thanks
I know that there are a lot of questions dealing with tkinter but I have looked at a bunch of them and none of them seem to help me.
import tkinter
class Calculator:
def __init__(self):
window = tkinter.Tk()
window.geometry("200x300")
window.title("Calculator")
lbl = tkinter.Label(window, text="placeholder", bg="blue", textvariable="labelText")
lbl.grid(row=0, column=0, columnspan=3)
self.firstNumArray = []
self.secondNumArray = []
self.operation = ""
self.currentNum = "first"
def appendNumber(self, number):
print("Appending Number")
if self.currentNum == "first":
self.firstNumArray.append(number)
print("".join(str(x) for x in self.firstNumArray))
lbl.config(text="".join(str(x) for x in self.firstNumArray))
window.update()
else:
self.secondNumArray.append(number)
for i in range(1,4):
string = "Creating button at ({0},{1})".format(0,i)
print(string)
button = tkinter.Button(text=i, command=lambda: appendNumber(self, i))
button.grid(row=1, column=i-1)
for i in range(1,4):
string = "Creating button at ({0},{1})".format(1,i)
print(string)
button = tkinter.Button(text=i+3, command=lambda: appendNumber(self, i+3))
button.grid(row=2, column=i-1)
for i in range(1,4):
string = "Creating button at ({0},{1})".format(2,i)
print(string)
button = tkinter.Button(text=i+6, command=lambda: appendNumber(self, i+6))
button.grid(row=3, column=i-1)
div = tkinter.Button(text="/")
mult = tkinter.Button(text="*")
add = tkinter.Button(text="+")
sub = tkinter.Button(text="-")
add.grid(row=1, column=3)
sub.grid(row=2, column=3)
mult.grid(row=3, column=3)
div.grid(row=4, column=3)
button = tkinter.Button(text="0")
button.grid(row=4, column=1)
window.mainloop()
calc = Calculator()
When I launch the program the window opens. When I click on a button the text in the label does not change. I have tried using a StringVar as the textvariable and then calling the set() function, but that did not work either. I think it has to do with the scope of the function. I had to place the appendNumber() function inside the __init__() because for some reason self.lbl = tkinter.Label() makes nothing pop up at all.
There are a few problems with your code.
labelText should, of course, be a StringVar and not a string...
labelText = tkinter.StringVar()
lbl = tkinter.Label(window, bg="blue", textvariable=labelText)
lbl.grid(row=0, column=0, columnspan=3)
Now you can use labelText.set to update the text. Also, no need for self parameter or window.update
def appendNumber(number):
if self.currentNum == "first":
self.firstNumArray.append(number)
labelText.set("".join(str(x) for x in self.firstNumArray))
else:
self.secondNumArray.append(number)
You can put all the buttons in one loop using // (integer (!) division) and % (modulo) operations. Also, be aware that the variable in the lambda is evaluated when the function is called, not when it is declared, i.e. all the lambdas will use the last value of i (9 in this case) -- see e.g. here. As a remedy, use lambda n=i+1: appendNumber(n).
for i in range(9):
btn = tkinter.Button(text=i+1, command=lambda n=i+1: appendNumber(n))
btn.grid(row=i//3+1, column=i%3)
Not really a problem, but since you don't need a reference to those buttons, you can make your code a bit more compact (same for the others):
tkinter.Button(text="/").grid(row=1, column=3)
I'm trying to make a label that will change when I enter text into the entry box and click the button.
I've tried doing some research but can't seem to find out how to do it .
from tkinter import *
master = Tk()
master.title("Part 3")
v = StringVar()
v.set("Please change me")
lb= Label(master, textvariable=v, fg="red",bg="black").grid(row=0,column=0)
ent= Entry(master, textvariable=v,).grid(row=1,column=2)
b1= Button(master, text="Click to change", fg="red",bg="black").grid(row=1,column=0)
to do so, you first need to define a callback that changes the value. (example below)
You should also use two Variables of type StringVar to store the different Values
from tkinter import *
master = Tk()
master.title("Part 3")
lText = StringVar()
lText.set("Please change me")
eText = StringVar()
def ChangeLabelText(event=None):
global lText
global eText
lText.set(eText.get())
Then, bind the callback to the button
lb = Label(master, textvariable=lText, fg="red",bg="black").grid(row=0,column=0)
ent = Entry(master, textvariable=eText).grid(row=1,column=2)
b1 = Button(master, text="Click to change", fg="red",bg="black", command=ChangeLabelText).grid(row=1,column=0)