I'm trying to make a program where you type text in the entry box, and then when you click a button it shows the text below, but it doesn't work, as soon as i click the button it gives me an error?
import sys
from tkinter import *
def myhello():
text = ment.get()
label = Label(text=entry).grid()
return
ment = str()
root = Tk()
root.title('Tutorial')
root.geometry('400x400')
button = Button(root, text='Button',command = myhello).place(x='160', y='5')
entry = Entry(textvariable=ment).place(x='5', y= '10 ')
root.mainloop()
You should use StringVar, not str.
You are using grid, place at the same time. Pick one.
import sys
from tkinter import *
def myhello():
text = ment.get()
label['text'] = text
root = Tk()
root.title('Tutorial')
root.geometry('400x400')
button = Button(root, text='Button',command=myhello).place(x='160', y='5')
label = Label(root, text='')
label.place(x=5, y=30)
ment = StringVar()
entry = Entry(textvariable=ment).place(x='5', y= '10 ')
root.mainloop()
Related
so this is my code right now and I was looking to see if you could help me.
from tkinter import *
from tkinter import messagebox
ad = Tk()
ad.geometry("300x300+500+200")
ed = Entry(ad)
ed.pack()
ed.focus_set()
def callback():
pass
button = Button(ad, text = "OK", width = 10, command = callback)
button.pack()
mainloop()
Frankly you should find it in many tutorials - ed.get()
You can use it to print() on screen or .append() to list.
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions ---
def callback():
print( ed.get() )
my_data.append( ed.get() )
print(my_data)
print('-----')
# --- main ---
my_data = []
root = tk.Tk()
ed = tk.Entry(root)
ed.pack()
ed.focus_set()
button = tk.Button(root, text="OK", command=callback)
button.pack()
root.mainloop()
PEP 8 -- Style Guide for Python Code
from tkinter import *
ad = Tk()
ad.geometry("300x300+500+200")
ed = Entry(ad)
ed.pack()
ed.focus_set()
list = Listbox (ad)
list.pack()
def callback():
text = ed.get()
list.insert(END,text)
button = Button(ad, text = "OK", width = 10, command = callback)
button.pack()
mainloop()
Try this. I just added the print format in the callback function. That's it.
from tkinter import *
from tkinter import messagebox
ad = Tk()
ad.geometry("300x300+500+200")
ed = Entry(ad)
ed.pack()
ed.focus_set()
def callback():
print(ed.get())
button = Button(ad, text = "OK", width = 10, command = callback)
button.pack()
mainloop()
What my code does currently is create a label with the text "Hello" every time I press the button that says "Say hello"
What I'm trying to figure out is how to create a button that clears off all of the labels off the screen, however I'm completely clueless.
How do I create a button that clears all of the labels off of the screen?
My code is down below.
import tkinter as tk
import time
root = tk.Tk()
root.geometry("700x500")
h = "Hello"
def CreateLabel():
helloLabel = tk.Label(root, text=h)
helloLabel.pack()
labelButton = tk.Button(root, text="Say hello", command=CreateLabel)
labelButton.pack()
root.mainloop()
I'm beginner but try this:
import tkinter as tk
import time
root = tk.Tk()
root.geometry("700x500")
h = "Hello"
liste = []
def CreateLabel():
helloLabel = tk.Label(root, text=h)
helloLabel.pack()
liste.append(helloLabel)
def DelLabel():
for i in range(len(liste)):
liste[i].destroy()
liste.clear()
labelButton = tk.Button(root, text="Say hello", command=CreateLabel)
labelButton.pack()
labelButton = tk.Button(root, text="del hello", command=DelLabel)
labelButton.pack()
root.mainloop()
So basically i want to make this GUI where you can insert text and then hit the go button ad it would do the feature. (example: "Stopwatch" and then after you hit enter it would start a stopwatch).
import tkinter as tk
import math
import time
root = tk.Tk()
root.geometry()
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text = "Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
main_entry = root.Entry(root, )
root.mainloop()
So basically I want the answer in main entry to be taken and do the action asked in the text insertion.
Here i wrote an example on how to use the Entry and the Button widgets in Tkitner:
from tkinter import *
from tkinter.messagebox import showerror
import random
class App(Tk):
def __init__(self):
super(App, self).__init__()
self.title('Get a random integer')
self.geometry('400x250')
Label(self, text='Get a random number from A to B').pack(pady=20)
self.a_value = Entry(self, width=10)
self.a_value.insert(END, 'A')
self.a_value.pack(pady=5)
self.b_value = Entry(self, width=10)
self.b_value.insert(END, 'B')
self.b_value.pack(pady=5)
self.btn = Button(self, text='Get Random', command=self.get_rand)
self.btn.pack(pady=5)
self.out_label = Label(self)
self.out_label.pack(pady=10)
self.bind('<Return>', self.get_rand)
def get_rand(self, event=None):
a = self.a_value.get()
b = self.b_value.get()
try:
a = int(a)
b = int(b)
self.out_label.configure(text=f'{random.randint(a, b)}')
self.update()
except Exception as error:
showerror(title='ERROR', message=f'{error}')
if __name__ == '__main__':
myapp = App()
myapp.mainloop()
import tkinter as tk
def quit():
global root
root.quit()
def prnt():
global usrinpt
lbl = tk.Label(text = usrinpt )
lbl.pack()
root = tk.Tk()
usrentr = tk.Entry()
usrinpt = str(usrentr.get())
usrentr.pack()
extbt = tk.Button(command=quit,text = 'Exit')
extbt.pack()
lblbt = tk.Button(command = prnt, text = 'Label')
lblbt.pack()
root.mainloop()
When I hit the Label button it just extends the window and doesn't print anything.
Thanks for the help!
You're adding the entry box then instantly trying to get the contents in the subsequent line. You should instead add a "submit" button that has the command set to star(usrentr.get())
The program is meant to review a set of sentences one at a time. I want to show one and then when the "next" button is clicked, it shows the next input. Right now it blasts through them. How do I get it to stop? I have a feeling I'm missing something small.
So here's the code:
from Tkinter import *
import ttk
root = Tk()
def iterate(number):
return number + 1
inputs = open("inputs.txt").readlines
lines = inputs()
numlines = len(lines)
x=0
for tq in lines:
sentence = lines[x].strip('\n')
sen = StringVar()
sen.set(sentence)
x = iterate(x)
ttk.Label(textvariable = sen).grid(column=1, row=1, columnspan=99)
ttk.Button(text = "next", command = x).grid(column=99, row=5, pady=5)
root.update()
root.mainloop()
To change what is displayed in a label you can call the configure method, giving it any of the same arguments you give when you create it. So, you would create a single label, then call this method to modify what is displayed.
The basic logic looks like this:
def do_next():
s = get_next_string_to_display()
the_label.configure(text=s)
the_label = ttk.Label(...)
the_button = ttk.Button(..., command=do_next)
This is the code I ultimately used to solve the issue:
from Tkinter import *
import ttk
root = Tk()
root.title("This space intentionally left blank")
root.minsize(800,200)
mainframe = ttk.Frame(root)
mainframe.grid(column = 0, row = 0)
def nextInputs(*args):
sen.set(inputs())
inputs = open("inputs.txt").readline
sen = StringVar()
ttk.Label(mainframe, textvariable=sen).grid(column=1, row=1, columnspan=99)
Button = ttk.Button(mainframe, text = "next", command = nextInputs).grid(column=99, row=5, pady=5)
root.bind('<Return>', nextInputs)
root.mainloop()