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()
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.
from tkinter import *
import os
os.system ("clear")
root = Tk()
root.title('Test')
root.geometry('1000x600')
def submit():
submit_text = Label(text=(eq))
submit_text.pack()
label = Label(root, text='Enter the first mounth')
label.pack()
textbox3 = Entry(root, width=30)
textbox3.pack()
textbox2 = Entry(root, width=30)
textbox2.pack()
textbox1 = Entry(root, width=30)
textbox1.pack()
eq = textbox1.get() + textbox2.get() + textbox3.get()
button = Button(root, text="submit", command=submit)
button.pack()
root.mainloop()
I want the code to open up a window and I'm going to put 3 numbers and I want the code to add the 3 numbers and show me the result when II press the button, but it does nothing.
I'm starting to learn Tkinter library and I have a problem...
I use grid to set my window the way I want but I can't figure out how I can set the width of the entry widget the same as the text widget.
When I put the same number, I don't have the same width anyway...
Here is my code :
from tkinter import *
def click():
try:
output.delete(0.0,END)
entered_text=entry.get()
output.insert(END, entered_text)
except:
output.insert(END, "")
def reset():
output.delete(0.0,END)
entry.delete(0,END)
if __name__ == '__main__':
window = Tk()
window.title("TEST")
window.geometry("500x500")
Label (window, text="Nombre de palettes :").grid(row=0, sticky=W)
Label (window, text="Prix :").grid(row=1, sticky=W)
entry = Entry (window)
entry.grid(row=0, column=2)
output = Text(window, width=8, heigh=1, wrap=WORD)
output.grid(row=1, column=2)
accepter=Button(window, text="Accepter", width=6, command=click)
accepter.grid(row=2, column=0)
restart = Button(window, text="Reset", width=6,command=reset)
restart.grid(row=2, column=1)
fin = Button(window, text="Quitter", width=6,command=window.destroy)
fin.grid(row=2, column=2)
window.grid_columnconfigure(4, minsize=100)
window.mainloop()
Thank you in advance.
One way is to expand the widgets to fill the cell:
entry = Entry(window)
entry.grid(row=0, column=2, sticky=E+W, padx=10)
output = Text(window, width=8, heigh=1, wrap=WORD)
output.grid(row=1, column=2, sticky=E+W, padx=10)
Where sticky=E+W fills the cell horizontally, and then I add some padding padx=10 to get a distance from the cell limits.
If one of the widgets always is bigger you can let that widget determine the cell width and then just expand the other widget.
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()
?