This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
So I've been having a weird bug with Python and Tkinter where a message box pops up too early. I don't know what's wrong: it should be working, right?
from tkinter import *
from tkinter import messagebox
with open("passwords.passwords", "a+") as f:
root = Tk()
root.geometry("1000x1000")
root.wm_title("Info Collect")
mylist = []
var1 = StringVar()
var1.set("Application:")
label1 = Label(root, textvariable=var1, height=2)
label1.grid(row=0, column=0)
var2 = StringVar()
var2.set("Password:")
label2 = Label(root, textvariable=var2, height=2)
label2.grid(row=0, column=3)
ID1 = StringVar()
ID2 = StringVar()
box1 = Entry(root, bd=4, textvariable=ID1)
box1.grid(row=0, column=1)
box2 = Entry(root, bd=5, textvariable=ID2)
box2.grid(row=0, column=4)
def get_info():
f.write("{0}: {1} ".format(box1.get(), box2.get()))
def output_info():
messagebox.showinfo(f.read())
buttonA = Button(root, text="Save info", command=get_info, width=8)
buttonA.grid(row=0, column=2)
buttonB = Button(root, text="Output info", command=output_info(), width=8)
buttonB.grid(row=0, column=5)
root.mainloop()
That is all the code, do I have to do anything to it?
Change this
buttonB = Button(root, text="Output info", command=output_info(), width=8)
to
buttonB = Button(root, text="Output info", command=output_info, width=8)
with parenthesis, the function is called and the returned value will be set to command, when you remove parenthesis you pass the function itself to command to be called when the button is clicked
Related
I want to update getting results from an entry box in a way that when an integer enters, the equivalent rows of entry boxes appear below that. I have written the below code to make it work using a button. However, I want to make it happen automatically without a button as I entered the number, the rows update. I checked one way of doing that is using the after(). I placed after after() in the function and out of the function but it is not working.
from tkinter import *
root = Tk()
root.geometry("400x400")
n_para = IntVar()
label1 = Label(root, text="Numeric parameters")
label1.grid(row=0, column=0)
entry1 = Entry(root, textvariable=n_para)
entry1.grid(row=0, column=1)
def update():
for i in range(1, n_para.get()+1):
entryX = Entry(root)
entryX.grid(row=i+1, column=0)
entryY = Entry(root)
entryY.grid(row=i+1, column=1)
entryZ = Entry(root)
entryZ.grid(row=i+1, column=2)
root.after(100, update)
root.after(1, update)
button1 = Button(root, text="update", command=update)
button1.grid(row=1, column=0)
root.mainloop()
You should try using the <KeyRelease> event bind.
import tkinter as tk
def on_focus_out(event):
label.configure(text=inputtxt.get())
root = tk.Tk()
label = tk.Label(root)
label.pack()
inputtxt = tk.Entry()
inputtxt.pack()
root.bind("<KeyRelease>", on_focus_out)
root.mainloop()
This types the text entered in real-time.
Edited Code with OP's requirement:
from tkinter import *
root = Tk()
root.geometry("400x400")
n_para = IntVar()
label1 = Label(root, text="Numeric parameters")
label1.grid(row=0, column=0)
entry1 = Entry(root, textvariable=n_para)
entry1.grid(row=0, column=1)
def upd(event):
x = entry1.get()
if not x.isnumeric():
x = 0
for i in range(1, int(x)+1):
entryX = Entry(root)
entryX.grid(row=i+1, column=0)
entryY = Entry(root)
entryY.grid(row=i+1, column=1)
entryZ = Entry(root)
entryZ.grid(row=i+1, column=2)
# root.after(100, update)
root.bind("<KeyRelease>", upd)
# button1 = Button(root, text="update", command=update)
# button1.grid(row=1, column=0)
root.mainloop()
This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 1 year ago.
I'm working through a youtube tutorial on Tkinter. Everything has been going fine but now I've hit a roadblock. The guy is showing how to use the .get() method to create a label with the text the user enters into an entry when the user hits a button. Save from the names of the label/entry/button, all my code is identical to his in function. It works for him, but I get a nonetype error. Here is a link to the video with a timestamp. Can anyone help me please?
URL: https://www.youtube.com/watch?v=YXPyB4XeYLA
Time: 33:30
from tkinter import *
import random
root = Tk()
root.title("Simple Routines")
Entry1 = Entry(root, width=20, bg='grey').grid(row=2, column=1)
def onClick():
Name = Entry1.get()
Label2 = Label(root, text=Name)
Label2.grid(row=4, column=1)
Label1 = Label(root, text="What Is Your Name?").grid(row=1, column=1)
Button1 = Button(root, text="Enter Your Name", bg='grey', command=onClick).grid(row=3, column=1)
root.mainloop()
This is because you have used .grid() on your textbox in the same line. In Tkinter if you place your widgets in the same line in which you create them it makes the widgets inaccessible later. I have attached the code for your reference.
from tkinter import *
import random
root = Tk()
root.title("Simple Routines")
Entry1 = Entry(root, width=20, bg='grey')
Entry1.grid(row=2, column=1) # The change
def onClick():
Name = Entry1.get()
Label2 = Label(root, text=Name)
Label2.grid(row=4, column=1)
Label1 = Label(root, text="What Is Your Name?").grid(row=1, column=1)
Button1 = Button(root, text="Enter Your Name", bg='grey', command=onClick).grid(row=3, column=1)
root.mainloop()
This question already has answers here:
Why isn't this label changing when I use the tkinter config option
(2 answers)
Closed 1 year ago.
I want this label to configure into the text entry after the user enters the text and hits go but the label isn't configuring.
I want the label that says "Hello!" to change into whatever is put in the main entry. I'm looking for an answer written in full code instead of one fixed line.
Here's my code:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
answer_label.config(text=main_entry.get())
entry_frame = tk.Frame(root)
main_entry = tk.Entry(entry_frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(entry_frame, text= 'Go!', width=85, command= answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hello!").pack()
entry_frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
1.Split tk.Label and pack().
2.Pass the lable.
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer(answer_label):
answer_label.config(text=main_entry.get())
entry_frame = tk.Frame(root)
main_entry = tk.Entry(entry_frame, width=100)
main_entry.grid(row=0, column=0)
answer_label = tk.Label(text = "Hello!")
answer_label.pack()
go_button = tk.Button(entry_frame, text= 'Go!', width=85, command=lambda: answer(answer_label))
go_button.grid(row=1, column=0)
entry_frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
I tried to change the label text by pressing the "+" button, but if I run the program it is already changed to "testok" instead of "test" at start. So my question is why?
from tkinter import *
root = Tk()
var = "test"
label = Label(root, text=var)
label.pack()
button_plus = Button(root, text="+", command=label.config(text=var + "ok"))
button_plus.pack()
button_minus = Button(root, text="-", command=root.destroy)
button_minus.pack()
root.mainloop()
The command of button_plus is assigned the result of label.config(text=var+"ok") which is None. You can use lambda to do what you want:
button_plus = Button(root, text="+", command=lambda: label.config(text=var + "ok"))
I am making a quiz in python using the tkinter module and I am stuck on how to create a button that checks to see if the answer is correct or not. But I would put it in a procedure however the question is already in one.
import tkinter as tk
window = tk.Tk()
window.title("6 Questions")
window.geometry("500x150")
score = 0
def inst():
t = tk.Label(window, text="All you need to do is just answer each question with either a '1, 2, 3' or the actual word.")
t.pack()
def start():
root = tk.Tk()
root.title("question 1")
q = tk.Label(root, text="what type of input holds whole numbers")
q.pack()
a = tk.Label(root, text="1.) int")
a.pack()
b = tk.Label(root, text="2.) string")
b.pack()
c = tk.Label(root, text="3.) float")
c.pack()
ans = tk.Entry(root, width=40)
ans.pack()
#here is the button I want to verify the answer
sub = tk.Button(root, text="Submit")
sub.pack()
greet = tk.Label(window, text="Welcome to the 6 Question Quiz.")
greet.pack()
start = tk.Button(window, command=start, text="Start")
start.pack()
instr = tk.Button(window, text="Instructions", command=inst)
instr.pack()
end = tk.Button(window, text="Exit", command=exit)
end.pack()
Create a function that opens when the submit button is clicked and create RadioButtons rather than Labels.
Like this:
def gettingDecision():
if var.get() is 'True':
messagebox.showinfo('Congrats', message='You Are Correct.Score is {}'.format(score))
else:
messagebox.showinfo('Lose', message='You Are Wrong.')
Question1 = ttk.Label(frame1, text='Q.1.Where does a computer add and compare data ?')
Question1.grid(row=1, column=0, sticky=W)
var = StringVar()
Q1A = ttk.Radiobutton(frame1, text='[A] Hard disk', variable=var, value='False1')
Q1A.grid(row=2, column=0, sticky=W)
Q1B = ttk.Radiobutton(frame1, text='[B] Floppy disk', variable=var, value='False2')
Q1B.grid(row=3, column=0, sticky=W)
Q1C = ttk.Radiobutton(frame1, text='[C] CPU chip', variable=var, value='True')
Q1C.grid(row=4, column=0, sticky=W)
Q1D = ttk.Radiobutton(frame1, text='[D] Memory chip', variable=var, value='False3')
Q1D.grid(row=5, column=0, sticky=W)
submit = ttk.Button(frame1, text='Submit', command=gettingDecision)
submit.grid()
Please note that, ideal way to go would be using classes.
You can define a function, inside of a function.
import tkinter as tk
window = tk.Tk()
window.title("6 Questions")
window.geometry("500x150")
score = 0
def inst():
t = tk.Label(window, text="All you need to do is just answer each question with either a '1, 2, 3' or the actual word.")
t.pack()
def start():
def submit():
print (ans.get())
#or do whatever you like with this
root = tk.Tk()
root.title("question 1")
q = tk.Label(root, text="what type of input holds whole numbers")
q.pack()
a = tk.Label(root, text="1.) int")
a.pack()
b = tk.Label(root, text="2.) string")
b.pack()
c = tk.Label(root, text="3.) float")
c.pack()
ans = tk.Entry(root, width=40)
ans.pack()
#here is the button I want to verify the answer
sub = tk.Button(root, text="Submit", command=submit)
sub.pack()
greet = tk.Label(window, text="Welcome to the 6 Question Quiz.")
greet.pack()
startButton = tk.Button(window, command=start, text="Start")
startButton.pack()
instr = tk.Button(window, text="Instructions", command=inst)
instr.pack()
end = tk.Button(window, text="Exit", command=window.destroy)
end.pack()
window.mainloop()