Tkinter Label Text Not Appearing - python-3.x

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.

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 to change Back ground colour for Tkinter module?

Hello I am very new with the python module Tkinter, I have written the code for a simple text editor. But I am not able to figure out how to change the background colour.
Help would be welcome thx.
Code:
from tkinter import *
import tkinter as tk
# from tkinter import filedialog
# from tkinter import font
root = Tk()
root.title('Flax')
root.iconbitmap('E:\editor.ico')
root.geometry('1200x660')
# Main Frame
my_frame = Frame(root)
my_frame.pack(pady=5)
# ScrollBar
scroll = Scrollbar(my_frame)
scroll.pack(side=RIGHT, fill=Y)
# Text Box
text = Text(my_frame, width=98, height=25, font=("Helvetica", 13), selectbackground="grey", selectforeground="white", undo=True)
text.pack()
scroll.config(command=text.yview)
root.mainloop()
The parameter you need to use is bg = "blue" for example source.
Implemented into your code:
from tkinter import *
import tkinter as tk
# from tkinter import filedialog
# from tkinter import font
root = Tk()
root.title('Flax')
root.iconbitmap('E:\editor.ico')
root.geometry('1200x660')
# Main Frame
my_frame = Frame(root)
my_frame.pack(pady=5)
# ScrollBar
scroll = Scrollbar(my_frame)
scroll.pack(side=RIGHT, fill=Y)
# Text Box
text = Text(my_frame, width=98, height=25, font=("Helvetica", 13), bg="grey", fg="white", undo=True)
text.pack()
scroll.config(command=text.yview)
root.mainloop()

why does tkinter widgets doesnt appear in vscode?

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

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)

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