I am learning Python tkinter fundamentals, and cannot get my method called "Submit()" to argue the string value for my Entry1 variable.
I have tried the .get() method for Entry1, but the console says the get attribute does not exist for Entry1.
from tkinter import *
Window = Tk()
def Submit():
Answer = Entry1.text
if Answer == "byte":
print("correct")
Label(Window, text="What do you call 8 bits?").grid(row=0)
Entry1 = Entry(Window, text="").grid(row=1)
Button(Window, text="SUBMIT", command=Submit).grid(row=2)
Window.mainloop()
I figured it out. Thanks, though.
from tkinter import *
Window = Tk()
def Quiz():
if Answer1.get() == "8" :
print("correct")
Question1 = Label(Window, text="How many bits are in a Byte?").grid(row=0)
Answer1 = StringVar()
Entry1 = Entry(Window, textvariable=Answer1).grid(row=1)
Button1 = Button(Window, text="ANSWER", command=Quiz).grid(row=2)
Window.mainloop()
Related
I am a total newbie with GUIs and also tkinter.
I am trying to get 2 inputs from user through a GUI which I implemented with tkinter. After the user give inputs and press the button, I want to return to these 2 inputs and keep doing something else with these 2 inputs. I have checked Return values of Tkinter text entry, close GUI but it did not work for my case.
What I have tried is this:
_Gui.py
import tkinter as tk
def get_input():
entry1 = entry1.get()
entry2 = entry2.get()
return entry1, entry2
root = tk.Tk()
root.geometry("320x240+800+300")
label1 = tk.Label(text="Label1")
label1.config(font=('Helvetica bold',16))
label1.pack()
entry1 = tk.Entry(root)
entry1.config(font=('Helvetica bold',16))
entry1.pack()
label2 = tk.Label(text="Label2")
label2.config(font=('Helvetica bold',16))
label2.pack()
entry2 = tk.Entry(root)
entry2.config(font=('Helvetica bold',16))
entry2.pack()
button = tk.Button(root, text="Start", command=get_input)
button.config(font=('Helvetica bold',16))
button.pack()
root.mainloop()
And in main.py:
import _Gui as gui
if __name__ == "__main__":
a, b = gui.get_input()
something_else(a, b)
But it also did not work.
Thanks for your help in advance!
You can use root.destroy() to destroy the window, but the function get_input() will execute twice after you pressed the button (the other is after the window is destroyed, in your "main.py"), so if you destroyed the window in the first execution, you can't use entry1.get().
I suggest you use this:
(_Gui.py)
import tkinter as tk
root = tk.Tk()
root.geometry("320x240+800+300")
def get_input():
global a, b
a = entry1.get()
b = entry2.get()
root.destroy()
label1 = tk.Label(text="Label1")
label1.config(font=('Helvetica bold',16))
label1.pack()
entry1 = tk.Entry(root)
entry1.config(font=('Helvetica bold',16))
entry1.pack()
label2 = tk.Label(text="Label2")
label2.config(font=('Helvetica bold',16))
label2.pack()
entry2 = tk.Entry(root)
entry2.config(font=('Helvetica bold',16))
entry2.pack()
button = tk.Button(root, text="Start", command=get_input)
button.config(font=('Helvetica bold',16))
button.pack()
(main.py)
import _Gui as gui
if __name__ == "__main__":
gui.root.mainloop()
something_else(gui.a, gui.b)
(Sorry, my English is not very well.)
I want to set the answer using the set() method without writing it in the text attribute in the Label and also I want to change the message without displaying the messages over each other.
I want the new message to appear and the one before it to disappear. I tried to use the set() and get() methods but it used to return its default value without using the value in the set() method and I don't know why.
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk,Image
root= Tk()
root.title("Project")
root.geometry('500x600')
root.title('Choose Answer')
var = IntVar()
textt = StringVar()
print(textt.set('Clicked..........'))
def Result():
print(textt.set('You Clicked....................'))
if(var.get() == 1):
print(var.get())
print(textt)
textt.set('You Clicked')
resultText = Label(root, text=textt , fg='white',bg='gray', width=40)
resultText.place(x=100,y=200)
else:
textt.set('You did\'t Click')
resultText = Label(root, text=textt, fg='white',bg='grey', width=40)
resultText.place(x=100,y=200)
checkBox = Checkbutton(root, text='Did you Study Math', variable=var, bg='grey')
checkBox.place(x=50,y=50)
button = Button(root, text='Select', fg='white', bg='grey',font='sans-serif',command=Result)
button.place(x=50,y=100)
root.mainloop()
This should work:
from tkinter import *
root = Tk()
root.title("Project")
root.geometry('500x600')
root.title('Choose Answer')
var = IntVar()
textt = StringVar()
resultText = Label(root, textvariable=textt , fg='white',bg='gray', width=40)
resultText.place(x=100,y=200)
def Result():
print(var.get())
if var.get() == 1:
textt.set('You Clicked')
else:
textt.set('You didn\'t Click')
checkBox = Checkbutton(root, text='Did you Study Math', variable=var, bg='grey')
checkBox.place(x=50,y=50)
button = Button(root, text='Select', fg='white', bg='grey',font='sans-serif',command=Result)
button.place(x=50,y=100)
root.mainloop()
When you create resultText, instead of setting the text argument to textt, you need to set the textvariable argument to text. That way, the text of the variable is automatically changed when you change textt.
I removed all the lines where resultText was recreated and re-placed, and just created it once before defining Result(). Also, I removed from tkinter import messagebox, because you already import it when you call from tkinter import *. Just a note: wildcard imports are discouraged, so try to avoid using them in the future.
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 am trying to create a window with Tkinter but no window is being created and I am not receiving any error codes?
from tkinter import *
def login_window():
window=Tk()
window.title("Login")
info_lbl = Label(window)
info_lbl.grid(row=0, column=1)
username_lbl = Label(window, text='Username')
username_lbl.grid(row=1, column=1)
username_entry = Entry(window, width=10)
username_entry.grid(row=1, column=2)
password_lbl = Label(window, text='Password')
password_lbl.grid(row=2, column=1)
password_entry = Entry(window, width=10, )
password_entry.grid(row=2, column=2)
ok_button = Button(window, text='Login', command = menu_window)
ok_button.grid(row=3,column = 2,sticky =W)
Any help would be great!
Well I think u should add mainloop() inside your function as well as call your login_window something like this-
from Tkinter import *
def login_window():
window=Tk()
window.title("Login")
mainloop()
login_window()
It looks like you never entered the main Tkinter loop. To display that window, you could add this to the bottom of the function:
window.mainloop()
Take a look at this question and the accepted answer for a bit more information on the Tkinter main loop.
I am trying to create a tkinter GUI which assigns a value to a variable when a button is pressed, then returns that value to be used in the rest of the code:
from tkinter import *
def yes_command(ans):
ans = 'yes'
window.destroy()
return (ans)
def no_command(ans):
ans = 'no'
window.destroy()
return (ans)
window = Tk()
yes_no_label = Label(window, text="Yes or no?")
yes_no_label.grid(row=0, column=1)
YESbutton = Button(window, text="Yes", fg='green', command = lambda :yes_command(ans))
YESbutton.grid(row=1, column=0)
NObutton = Button(window, text = 'No', fg = 'red', command= lambda :no_command(ans))
NObutton.grid(row=1, column=2)
window.mainloop()
print(ans)
In this, the GUI comes up and once a button is pressed, it does close. However, it does not output (as this is called outside of the function I know it would work if it printed).
What further confuses me is that if I were to replace return (ans) with print (ans) it will print. Surely this just means it will not allow the value to leave the function? In which case, why?
I would be grateful for any help, so thanks in advance.
I have a simple solution for your question. I use a global StringVar() variable. You can set global variable in any function and print later.
from Tkinter import *
def yes_command():
global answer
answer.set('yes')
window.destroy()
def no_command():
global answer
answer.set('no')
window.destroy()
window = Tk()
yes_no_label = Label(window, text="Yes or no?")
yes_no_label.grid(row=0, column=1)
global answer
answer=StringVar()
YESbutton = Button(window, text="Yes", fg='green', command = lambda :yes_command())
YESbutton.grid(row=1, column=0)
NObutton = Button(window, text = 'No', fg = 'red', command= lambda :no_command())
NObutton.grid(row=1, column=2)
window.mainloop()
print(answer.get())
You can make a class and handle a global value from there. I also added a new button named Test ans value to read the value of ans everytime you push the Yes or No button. I also commented the .destroy() lines so you can check how it works.
from tkinter import *
class UI(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.ans = ''
yes_no_label = Label(window, text="Yes or no?")
yes_no_label.grid(row=0, column=1)
YESbutton = Button(window, text="Yes", fg='green', command =lambda:self.yes_command())
YESbutton.grid(row=1, column=0)
NObutton = Button(window, text = 'No', fg = 'red', command= lambda:self.no_command())
NObutton.grid(row=1, column=2)
checkValue = Button(window, text = 'Test ans value', command= lambda:self.test_value())
checkValue.grid(row=2, column=1)
def yes_command(self):
self.ans = 'yes'
#window.destroy()
def no_command(self):
self.ans = 'no'
#window.destroy()
def test_value(self):
print self.ans
window = Tk()
newUI = UI(window)
window.mainloop()
Thanks everyone for the ideas, I have taken a simple version, then tested it by calling it, adding it to a variable, then outputting that to test that the value re-assigns:
from tkinter import *
def YNquestion():
global answer
def yes_command():
answer.set('yes')
window.destroy()
def no_command():
answer.set('no')
window.destroy()
window = Tk()
yes_no_label = Label(window, text="Yes or no?")
yes_no_label.grid(row=0, column=1)
answer=StringVar()
YESbutton = Button(window, text="Yes", fg='green', command = lambda :yes_command())
YESbutton.grid(row=1, column=0)
NObutton = Button(window, text = 'No', fg = 'red', command= lambda :no_command())
NObutton.grid(row=1, column=2)
window.mainloop()
YNquestion()
choice = answer.get()
print(choice)
YNquestion()
choice = answer.get()
print(choice)
If I press Yes the first time around and No the second time around, it prints correctly. In this version I removed the unnecessary uses of global. It works perfectly, thanks for all the help!