I made wordle, but I want to make it look more like the official wordle so my final goal is to make 5 tkinter entries on every row, and have them linked to each other. When I run this code, mainloop at the bottom is greyed out and no window appears.
root = Tk()
root.geometry('400x400')
def testlen():
global textinentry1
textinentry1= entry1.get()
if len(textinentry1) >1 :
entry1.delete(0,END)
entry1.insert(0,textinentry1[0])
entry2.delete(0, END)
entry2.insert(0,textinentry1[1] )
entry1 = Entry(root,width=5, font = ('Georgia 18'), justify=CENTER)
entry1.grid(row=0, column=0)
entry2 = Entry(root, width=5, font = ('Georgia 18'), justify=CENTER)
entry2.grid(row=0, column=1)
entry3 = Entry(root, width = 5,font = ('Georgia 18'), justify=CENTER)
entry3.grid(row=0, column=2)
while True:
testlen()
root.mainloop()
What did I do wrong?
The mainloop is executed after while loop end Which is never end this is because you are not seeing the window here.
If you want to run this code then you can use the root.after() method to create a loop.
from tkinter import *
root = Tk()
root.geometry('400x400')
def testlen():
global textinentry1
textinentry1= entry1.get()
if len(textinentry1) >1 :
entry2.delete(0, END)
entry2.insert(0,textinentry1[1] )
entry1 = Entry(root,width=22, font = ('Georgia 18'), justify=CENTER)
entry1.grid(row=0, column=0)
entry2 = Entry(root, width=22, font = ('Georgia 18'), justify=CENTER)
entry1button = Button(root, text="Enter", command = lambda :testlen())
entry1button.grid(row=1, column=0)
entry2.grid(row=2, column=0)
entry3 = Entry(root, width = 22,font = ('Georgia 18'), justify=CENTER)
entry3.grid(row=4, column=0)
def loop():
testlen()
root.after(1,loop) # 1 is 1 millisecond. Here root.after method call the loop function after 1 millisecond without crashing your code.
loop()
root.mainloop()
Related
Below is my code. i would like to know how to start the program with the radio buttons not selected.
from tkinter import *
root = Tk()
root.title("Early Learning Tool")
root.geometry("800x400")
frame = LabelFrame(root, text="Letters", padx=30, pady=10)
frame.pack()
photo_A = PhotoImage(file=r"Pictures\A.png")
photo_B = PhotoImage(file=r"Pictures\B.png")
photo_C = PhotoImage(file=r"Pictures\C.png")
photo_D = PhotoImage(file=r"Pictures\D.png")
photo_E = PhotoImage(file=r"Pictures\E.png")
letter_choice = StringVar()
rb_A = Radiobutton(frame, variable=letter_choice, value="A", image=photo_A)
rb_A.grid(row=0, column=0)
rb_B = Radiobutton(frame, variable=letter_choice, value="B", image=photo_B)
rb_B.grid(row=0, column=1)
rb_C = Radiobutton(frame, variable=letter_choice, value="C", image=photo_C)
rb_C.grid(row=0, column=2)
rb_D = Radiobutton(frame, variable=letter_choice, value="D", image=photo_D)
rb_D.grid(row=0, column=3)
rb_E = Radiobutton(frame, variable=letter_choice, value="E", image=photo_E)
rb_E.grid(row=0, column=4)
root.mainloop()
This is what appears. How do I make it so it doesn't do that?
In letter_choice variable you have to set a default value in StringVar()
like so:
letter_choice = StringVar(value="A")
You need to initialize the StringVar with value other than empty string (default value if not specified) or the values for those radiobuttons, for example ' ':
letter_choice = StringVar(value=' ')
I tried running the programming switching the images to text.
from tkinter import *
root = Tk()
root.title("Early Learning Tool")
root.geometry("800x400")
frame = LabelFrame(root, text="Letters", padx=30, pady=10)
frame.pack()
letter_choice = StringVar()
rb_A = Radiobutton(frame, variable=letter_choice, value="A", text="A")
rb_A.grid(row=0, column=0)
rb_B = Radiobutton(frame, variable=letter_choice, value="B", text="B")
rb_B.grid(row=0, column=1)
rb_C = Radiobutton(frame, variable=letter_choice, value="C", text="C")
rb_C.grid(row=0, column=2)
rb_D = Radiobutton(frame, variable=letter_choice, value="D", text="D")
rb_D.grid(row=0, column=3)
rb_E = Radiobutton(frame, variable=letter_choice, value="E", text="E")
rb_E.grid(row=0, column=4)
root.mainloop()
I didn't encounter any problems. Indeed, the default radiobutton when no buttons are clicked, appears with a small grey dot. But when clicking on a radiobutton the default grey dots disappear.
I want to take user input and output it inside GUI ...
my code
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label = Label(root, text="???")
label.grid(row=1, column=2)
ok=Label(root, text="Type your name").grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
root.mainloop()
To get the text from an input box, use inputbox.get(). Also, don't set the ok variable to Label(root, text="Type your name").grid(row=2,column=1). This will be set as NoneType, so do
ok = Label(root, text="Type your name").grid(row=2,column=1)
ok.grid(row=2, column=1)
Here is your code:
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label = Label(root, text=entry.get())
label.grid(row=1, column=2)
ok=Label(root, text="Type your name")
ok.grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
root.mainloop()
First thing, In order to accept and display the output on the screen you have to use either Label widget or Canvas Text. Since your code is not updating the Label widget thus I am here doing what you want to do.
First, create a Label widget in the main window,
Get the user input by using.get() method,
print and display the user input.
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label.config(text=entry.get())
ok=Label(root, text="Type your name")
ok.grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
#Create a Label to print the Name
label= Label(root, text="", font= ('Helvetica 14 bold'), foreground= "red3")
label.grid(row=1, column=2)
root.mainloop()
Code
list = [ee1.get(), ee2.get(), ee3.get(), ee4.get(), ee5.get(), ee6.get(), ee7.get(), ee8.get(), ee9.get(), ee10.get()]
context = "What does", random.choice(list),"mean?"
labelquestion = Label(window, text = context, font = "Serif 10 bold")
Question
When I run this, it outputs "{What does} {} mean?"
How can I fix this so that it outputs "What does"-random entry from list-"this mean?"
(The things in the list are entries)
Below is an example based on your code and information in the comment:
import tkinter as tk
import random
window = tk.Tk()
ee1 = tk.Entry(window)
ee1.grid(row=0, column=0)
ee2 = tk.Entry(window)
ee2.grid(row=0, column=1)
ee3 = tk.Entry(window)
ee3.grid(row=0, column=2)
ee4 = tk.Entry(window)
ee4.grid(row=0, column=3)
ee5 = tk.Entry(window)
ee5.grid(row=0, column=4)
ee6 = tk.Entry(window)
ee6.grid(row=1, column=0)
ee7 = tk.Entry(window)
ee7.grid(row=1, column=1)
ee8 = tk.Entry(window)
ee8.grid(row=1, column=2)
ee9 = tk.Entry(window)
ee9.grid(row=1, column=3)
ee10 = tk.Entry(window)
ee10.grid(row=1, column=4)
def submit():
words = [ee1.get(), ee2.get(), ee3.get(), ee4.get(), ee5.get(),
ee6.get(), ee7.get(), ee8.get(), ee9.get(), ee10.get()]
context = f"What does '{random.choice(words)}' mean?"
labelquestion.config(text=context)
tk.Button(window, text="Submit", command=submit).grid(row=2, column=0, sticky='w')
labelquestion = tk.Label(window, font="Serif 10 bold")
labelquestion.grid(row=2, column=1, columnspan=4)
window.mainloop()
I've looked other questions and have tried to fix this but I am really struggling with having my code print back the answer.
from tkinter import *
root = Tk()
label1 = Label(root, text="Number 1")
label2 = Label(root, text="Number 2")
labelplus = Label(root, text="+")
label1.grid(row=0, sticky=E)
label2.grid(row=3, sticky=E)
labelplus.grid(row=2, column=1)
entry1_var= StringVar()
entry2_var= StringVar()
entry1 = Entry(root, textvariable= entry1_var)
entry2 = Entry(root, textvariable= entry2_var)
entry1.grid(row=0, column=1)
entry2.grid(row=3, column=1)
first = (entry1_var.get()
second =(entry2_var.get()
def additionStuff(event):
totalNumbers = (first + second)
print(totalNumbers)
button1 = Button(root, text="Add Numbers")
button1.bind("<Button-1>", additionStuff)
button1.grid(row=4, column=1)
root.mainloop()
Why does my function now print back the answer?
You need to call StringVar.get inside of additionStuff:
def additionStuff(event):
first = entry1_var.get()
second = entry2_var.get()
totalNumbers = (float(first) + float(second))
print(totalNumbers)
Otherwise you're getting the value of first and second before the user has had opportunity to enter anything.
I am in the process of learning basic Python. I am currently attempting to create a simple calculator program that only has addition and subtraction. I have one issue though. I am not sure how I would add text to my Python label upon button press. Right now, upon pressing the '1' button, my program will change the display label to the text "1". However, I want my program to add text, not set it.
For example, if I press 'button 1' 5 times, it currently will reset the label text 5 times and will result with a single 1. I want it to add the number to the label upon press, not replace.
Current Result after pressing button 5 times: 1
Requested result after pressing button 5 times: 11111
Here is my current code for the program. If anything is unclear, just ask; thanks.
from tkinter import *
window = Tk()
# Creating main label
display = Label(window, text="")
display.grid(row=0, columnspan=3)
def add_one():
display.config(text='1')
# Creating all number buttons
one = Button(window, text="1", height=10, width=10, command=add_one)
two = Button(window, text="2", height=10, width=10)
three = Button(window, text="3", height=10, width=10)
four = Button(window, text="4", height=10, width=10)
five = Button(window, text="5", height=10, width=10)
six = Button(window, text="6", height=10, width=10)
seven = Button(window, text="7", height=10, width=10)
eight = Button(window, text="8", height=10, width=10)
nine = Button(window, text="9", height=10, width=10)
zero = Button(window, text="0", height=10, width=10)
# Placing all number buttons
one.grid(row=1, column=0)
two.grid(row=1, column=1)
three.grid(row=1, column=2)
four.grid(row=2, column=0)
five.grid(row=2, column=1)
six.grid(row=2, column=2)
seven.grid(row=3, column=0)
eight.grid(row=3, column=1)
nine.grid(row=3, column=2)
# Creating all other buttons
add = Button(window, text="+", height=10, width=10)
subtract = Button(window, text="-", height=10, width=10)
equal = Button(window, text="=", height=10, width=10)
# Placing all other buttons
add.grid(row=4, column=0)
subtract.grid(row=4, column=1)
equal.grid(row=4, column=2)
window.mainloop()
You should use a StringVar for this. And your callback needs to get the current contents of the StringVar, modify it, and use the modified string to set the new value of the StringVar. Like this:
import tkinter as tk
window = tk.Tk()
# Creating main label
display_text = tk.StringVar()
display = tk.Label(window, textvariable=display_text)
display.grid(row=0, columnspan=3)
def add_one():
s = display_text.get()
s += '1'
display_text.set(s)
one = tk.Button(window, text="1", height=10, width=10, command=add_one)
one.grid(row=1, column=0)
window.mainloop()
BTW, you should try to make your program a little more compact by using for loops to create and lay out your buttons, in accordance with the DRY principle.
Also, it's not a good idea to use from tkinter import *. It imports over 130 names into your namespace, making it easy to create name collisions if you accidentally use a Tkinter name for one of your own variables or functions.
You can define add_one like the following, to first get the existing value and then append a new value to it:
def add_one():
current_value = display.cget("text")
new_value = current_value + "1"
display.config(text=new_value)
Is this what you're looking for:
from tkinter import *
root = Tk()
var = StringVar()
def f1():
var.set(" ")
var.set("1")
def f2():
var.set(" ")
var.set("2")
label = Label(root, textvariable=var)
label.pack()
button1 = Button(root, text="One", command=f1)
button1.pack()
button2 = Button(root, text="Two", command=f2)
button2.pack()
?