Tkinter doesn't print on window - python

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())

Related

How can I prevent my main window from running with a Toplevel window in python and Tkinter?

I am trying to stop the main window from running until a button has been pressed on a separate Toplevel window.
Example:
from tkinter import *
let_user_through = False
window = Tk()
def activate_main_window():
global let_user_through
let_user_through = True
frame = Toplevel()
b = Button(frame, text="Enter", command=activate_main_window).pack()
if let_user_through == True:
lbl = Label(window, text="Hello")
#bunch of code
#bunch of code
window.mainloop()
In this example, in the main window there is a label that reads: "Hello".
But I don't want people to be able to see it if they haven't pressed the button on the frame
Once the user has pressed the button, the frame will destroy itself and the main window will continue executing a bunch of code.
I'm a beginner to tkinter so i'm not sure if the answer is obvious or not. Thanks!
You can use frame.wait_window() to wait until frame is destroyed. Also you need to call frame.destroy() inside activate_main_window().
from tkinter import *
let_user_through = False
window = Tk()
def activate_main_window():
global let_user_through
let_user_through = True
frame.destroy() # need to destroy frame
# wait for root window becomes visible
# otherwise "frame" may be open behind root window
window.wait_visibility()
frame = Toplevel()
Button(frame, text="Enter", command=activate_main_window).pack()
frame.grab_set() # capture keyboard/mouse events
frame.wait_window() # wait for "frame" to be destroyed
if let_user_through:
Label(window, text="Hello").pack()
#bunch of code
#bunch of code
# should it be within the above for loop?
window.mainloop()
A small change to your code using window.withdraw and window.deiconify works for me. #acw1668 correctly pointed out an error in my original code, so here is the fix.
Your main window is invisible until user presses button.
import tkinter as tk
let_user_through = False
window = tk.Tk()
window.withdraw()
def activate_main_window():
global let_user_through
let_user_through = True
frame.destroy() # need to destroy frame
frame = tk.Toplevel()
tk.Button(frame, text="Enter", command=activate_main_window).pack()
frame.wait_window() # wait for "frame" to be destroyed
if let_user_through:
tk.Label(window, text="Hello").pack()
window.update()
window.deiconify()
#bunch of code
#bunch of code
window.mainloop()
I've created a class that removes the need for let_user_through and sets up code for any next steps.
import tkinter as tk
class invisible:
def __init__( self ):
self.window = tk.Tk()
self.window.withdraw() # make window invisible
self.frame = tk.Toplevel()
tk.Button(
self.frame, text = "Enter", command = self.activate_main_window ).pack( fill='both' )
self.frame.wait_window( ) # wait for "frame"
self.button = tk.Button( self.window, text = "Hello", command = self.remove_next )
self.button.pack( fill = 'both')
self.window.update()
self.window.deiconify() # make window visible
def activate_main_window( self ):
self.frame.destroy() # need to destroy frame
def remove_next( self ):
self.button.destroy()
tk.Label( self.window, text = "Bunch of codeA" ).pack( fill = 'both' )
tk.Label( self.window, text = "Bunch of codeB" ).pack( fill = 'both' )
tk.Label( self.window, text = "Bunch of codeC" ).pack( fill = 'both' )
# continue code initialization
if __name__ == '__main__':
make = invisible()
tk.mainloop()

How do I create a button that clears off all labels in tkinter?

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()

How to expand entry box with window in tkinter?

I'm just creating simple window using tkinter which have entry box and search button. What is want is when i maximize window search bar also starch but it is not happening,
Here is my code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.title("Wikipedia")
class first:
def labels(self):
search_frame = ttk.LabelFrame(root)
search_frame.pack(side = "top",fill = "both")
search_var = tk.StringVar()
search_bar = tk.Entry(search_frame,width = 40,textvariable = search_var)
search_bar.grid(row = 0,column = 0)
search_button = ttk.Button(search_frame,text = "Search")
search_button.grid(row = 1,column = 0)
boot = first()
boot.labels()
root.mainloop()
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.title("Wikipedia")
class first:
def labels(self):
search_frame = ttk.LabelFrame(root)
search_frame.pack(side="top", fill="both")
search_var = tk.StringVar()
search_bar = tk.Entry(search_frame, width=20, textvariable=search_var)
search_bar.pack(fill="x")
search_button = ttk.Button(search_frame, text="Search")
search_button.pack()
boot = first()
boot.labels()
root.mainloop()
Not sure this is what you're looking for but try it out.
Here I have used fill='x' in the .pack() geometry manager to fill the row

Python Tkinter - Close dialog without closing main window

I'm trying to make a text entry dialog with Tkinter (Python 3.5) but I'm having some problems. This is my code:
class TextEntryDialog:
def __init__(self, master):
self.top = Toplevel(master)
self.textField = Entry()
self.textField.pack()
root = Tk()
ted = TextEntryDialog(root)
root.mainloop()
When I run this I get a dialog and a main window just like I want, but the problem is that when I close the dialog the main window closes as well. I would like the main window to stay open when the dialog closes, can anyone help me with this?
Add titles to windows and you see
You add Entry to MainWindow.
And you close MainWindow but you think it is TextEntryDialog.
You have to add self.top (Toplevel) as parent in Entry to put it in correct window.
self.textField = Entry(self.top)
.
from tkinter import *
class TextEntryDialog:
def __init__(self, master):
self.top = Toplevel(master)
self.top.title("TextEntryDialog")
self.textField = Entry(self.top) # parent
self.textField.pack()
root = Tk()
root.title("MainWindow")
ted = TextEntryDialog(root)
root.mainloop()
You may want to restructure your code. The following sample application demonstrates how to open a dialog for text entry and prevent closure of the main window when the dialog finishes executing:
from tkinter import Label, NoDefaultRoot, Tk
from tkinter.font import Font
from tkinter.simpledialog import askstring
def main():
NoDefaultRoot()
root = Tk()
root.title('Demonstration')
root.resizable(False, False)
Label(root, font=Font(root, size=24)).grid()
root.after_idle(animate_label, root, 3)
root.mainloop()
def animate_label(root, time_left):
label = get_any_child(root, Label)
label['text'] = 'Opening a dialog in {} ...'.format(max(time_left, 0))
if time_left > 0:
root.after(1000, animate_label, root, time_left - 1)
else:
root.after_idle(open_dialog, root)
def get_any_child(widget, kind):
return get_any(get_children(widget), kind)
def get_children(widget):
return iter(widget.children.values())
def get_any(iterable, kind):
return next(item for item in iterable if isinstance(item, kind))
def open_dialog(root):
answer = askstring('Text Entry', 'Who are you?', parent=root)
label = get_any_child(root, Label)
if answer:
label['text'] = 'You are {}.'.format(answer)
else:
label['text'] = 'I must find out who you are.'
root.after(3000, open_dialog, root)
if __name__ == '__main__':
main()

Why won't the text display

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()

Categories