why does tkinter widgets doesnt appear in vscode? - python-3.x

from tkinter import *
from tkinter import ttk
root = Tk()
root.mainloop()
button1 = ttk.Label(root, text = 'lol')
button1.pack()
And when i try to run the program only the window show up but without the button.
it gives me this error:
Exception has occurred: TclError
NULL main window
File "C:\Users\Elad\Desktop\coding\tkintertut.py", line 6, in <module>
button1 = ttk.Label(root, text = 'lol')

The root.mainloop() has to be at the end of the code for the window to be shown, not right after declaration of root.
from tkinter import *
from tkinter import ttk
root = Tk()
button1 = ttk.Label(root, text = 'lol')
button1.pack()
root.mainloop()
Explanation:
Only the lines of code between root and root.mainloop() gets executed as long as the window is open, if you close the window, the Labels and all other widget declared after root.mainloop() become active but, the window is now closed and root is destroyed, hence the error. But here in my answer, the code between root and root.mainloop() has everything I need to be in the window and hence it shows the complete window. Just keep in mind to always say root.mainloop() at the end of the code only
Hope it cleared your errors. Do let me know if any more errors or doubts.
Cheers

Your root.mainloop() should be the last code!
from tkinter import *
from tkinter import ttk
root = Tk()
button1 = ttk.Label(root, text = 'lol')
button1.pack()
root.mainloop()

Related

Tkinter - How to understand when the cursor is placed over a text widget?

I would very like to do something when the mouse cursor is placed over a TK Text widget (without clicking). How can I get this event?
Tkinter fires the <Enter> event with the cursor enters a widget, and <Leave> when it leaves.
import tkinter as tk
def handle_enter(event):
event.widget.insert("end", "Cursor has entered the chat\n")
def handle_leave(event):
event.widget.insert("end", "Cursor has left the chat\n")
root = tk.Tk()
text = tk.Text(root)
text.pack(fill="both", expand=True)
text.bind("<Enter>", handle_enter)
text.bind("<Leave>", handle_leave)
root.mainloop()
Try the following code. It is a quick snippet:
#Import the tkinter library
from tkinter import *
from tkinter.tix import *
#Create an instance of tkinter frame
win = Tk()
#Set the geometry
win.geometry("400x200")
#Create a tooltip
tip= Balloon(win)
#Create a Button widget
my_button=Button(win, text= "Python", font=('Poppins', 20))
my_button.pack(pady=20)
#Bind the tooltip with button
tip.bind_widget(my_button,balloonmsg="Python is an interpreted, high-level
and general-purpose programming language")
win.mainloop()

How do I display a message before closing the tkinter window?

A message is required to be shown just before closing the tkinter window on the click of a close button added to the window.
lab = Label(window,text = 'thank you')
lab.grid()
window.destroy()
I used the above code to do so, but the window gets closed before the message is being displayed
Can I have the solution for this?
You can either use this:
from tkinter import Tk
import tkinter.messagebox as msgbox
def display_msg():
msgbox.showinfo(title='', message='Thank You')
root.destroy()
root = Tk()
root.protocol('WM_DELETE_WINDOW', display_msg)
root.mainloop()
which will show You a messagebox before closing,
or if You want to display a widget use this:
from tkinter import Tk, Label
def display_msg():
msg = Label(root, text='Thank You!')
msg.pack()
root.after(3000, root.quit)
root = Tk()
root.protocol('WM_DELETE_WINDOW', display_msg)
root.mainloop()
When You protocol root with 'WM_DELETE_WINDOM' You can make it execute a function when You try to close the window.
In the first example it will just show an infobox.
In the second example it will wait for 3 seconds (3000 miliseconds) and then destroy the root

Tkinter Toplevel window not appearing

Python 3.8, Win 10 is the os, Toplevel widget does not appear to be working with new window not appearing. Any guidance would be appreciated, thanks!
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
top.mainloop()
my_button = Button(root, text="Popup, click here", command="popup")
my_button.grid(row=0, column=0)
root.mainloop()
Problem:
The only issue here is that the callback command shouldn't be a string.
Solution:
Remove the quotes around popup and the Toplevel window should appear.
Fixed Code:
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
my_label_top.pack()
my_button = Button(root, text="Popup, click here", command=popup)
my_button.grid(row=0, column=0)
root.mainloop()
Tips:
Using top.mainloop() is not necessary.
You also forgot to pack() the Label(my_label_top)

Tkinter Label Text Not Appearing

I use this code (from https://www.python-course.eu/tkinter_labels.php) but no text appears in the label.
import tkinter as tk
root = tk.Tk()
w = tk.Label(root, text="Hello Tkinter!")
w.pack()
root.mainloop()
I suspect that any problem may involve the mainloop function.

How to get input from a function in Python and print in tkinter GUI?

from tkinter import *
def printSomething():
inputValue=textBox.get("1.0","end-1c")
res=response(inputValue)
label = Label(root, text=res)
#this creates a new label to the GUI
label.pack()
root = Tk()
button = Button(root, text="Print Me", command=printSomething)
button.pack()
textBox=Text(root, height=2, width=10)
textBox.pack()
root.mainloop()
I have written a python code that returns text. and print that in tkinter label.while i try to execute it shows "None" in label.
It would probably be better to create the label in the global namespace once and then just update the label every time you press the button.
I also recommend using import tkinter as tk vs from tkinter import * as it provides better maintainability as your code grows and you do not end up overwriting built in methods.
I have updated your code and changed a few things to better fit the PEP8 standard.
import tkinter as tk
def print_something():
label.config(text=text_box.get("1.0", "end-1c"))
root = tk.Tk()
tk.Button(root, text="Print Me", command=print_something).pack()
text_box = tk.Text(root, height=2, width=10)
text_box.pack()
label = tk.Label(root)
label.pack()
root.mainloop()
Just changing your line:
res = response(inputValue)
to
res = inputValue
worked for me, creating a new label every time I pressed the button.

Resources