Tkinter Label not Showing on MacOS - python

I'm currently learning Tkinter and cannot find a solution to my problem. The Tkinter Label is not showing up in the window and no one has the solution why. I am on MacOS M1 Pro.
from tkinter import *
root = Tk()
# Create label widget
myLabel = Label(root, text="Hello World!")
# Pack it onto the screen
myLabel.pack()
root.mainloop()
Shown Result:

Try adding size to your root window.
from tkinter import *
root = Tk()
root.geometry("500x500")
# Create label widget
myLabel = Label(root, text="Hello World!")
# Pack it onto the screen.
myLabel.pack()
root.mainloop()

Related

Tkinter Label not showing text

I am learning how to use tkinter. When I try doing an hello world example, text does not appear. A small window pops up empty.
My basic code goes like this:
from tkinter import *
root = Tk()
label = Label(root, text="Hello World!")
label.pack()
root.mainloop()

Why does tkinter creating two windows here? And how can i stop it?

import tkinter as tk
from subprocess import check_call
def copy_name():
cmd = 'echo ' + name.strip() + '|clip'
return check_call(cmd, shell=True)
root = tk.Toplevel(background="black")
root.title("Copying")
root.resizable(False, False)
T = tk.Label(root, text=name, height=2, width=len(name) + 25, background="black", foreground="white")
T.pack()
button = tk.Button(root, text="Copy", command=copy_name, background="black", foreground="white")
button.pack()
tk.mainloop()
This is my code.
I just wanted to test this way of copying text...
About my expectations... i want to understand from where those windows are appearing, and how to stop it.
Im just a newbie in Python and Tkinter... so please, tell me what i did wrong
You are not using Toplevel(). You just wanted single window. Just replaced Toplevel to tk()
Code:
import tkinter as tk
from subprocess import check_call
def copy_name():
cmd = 'echo ' + name.strip() + '|clip'
return check_call(cmd, shell=True)
root = tk.Tk()
root.title("Copying")
root.resizable(False, False)
T = tk.Label(root, text='name', height=2, width=25, background="black", foreground="white")
T.pack()
button = tk.Button(root, text="Copy", command=copy_name, background="black", foreground="white")
button.pack()
tk.mainloop()
Screenshot:
Every tkinter window requires a root window - an instance of Tk. If you don't create one, one will be created automatically. When you do root = tk.Toplevel(background="black"), tkinter will first create an instance of Tk and then it will create your Toplevel, resulting in two windows.
The solution in this case is to call Tk instead of Toplevel. Also, you'll need to remove the background="black" argument and instead configure the background in a separate step.
root = tk.Tk()
root.configure(background="black")
As #Bryan said, you should forget about Toplevel(). The normal way is Tk().
Try this:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
label = tk.Label( root, text='Select:', background='green').pack(side='top')
btn1 = ttk.Button( root, text='Discard').pack()
btn2 = ttk.Button( root, text='Quit').pack()
while True:
root.mainloop()
And you should get:

My browse window in tkinter goes under the toplevel tkinter window where topmost enabled

I will try to simplify my question here. My problem is like, on my first window of tkinter I have a button that opens another tkinter window. You can say it a second window to keep it on top I use win2.attributes('-topmost', True). Then I have a browse button to import file from the computer but when I click it goes under the window 2.
Following is my code.
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.title("Base Window")
root.geometry("300x300")
def browse():
file_to_open = askopenfilename()
def new_window():
win2 = Toplevel(root)
win2.title("Window 2")
win2.geometry("300x300")
win2.attributes('-topmost', True)
Button2 = Button(win2, text="Browse", command=browse).pack()
button1 = Button(root, text="New window", command=new_window).pack()
root.mainloop()
My browse window is going under the window 2. Can you please suggest me the best way to keep it on top. I am sharing the screenshot of the problem as well
Starting code
Click on new window
Click browse button and the browse window is hidden
Cheers
You need to set the parent option of askopenfilename() to the toplevel window win2:
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.title("Base Window")
root.geometry("300x300")
def browse(parent):
file_to_open = askopenfilename(parent=parent)
def new_window():
win2 = Toplevel(root)
win2.title("Window 2")
win2.geometry("300x300")
win2.attributes('-topmost', True)
# pass 'win2' to browse()
Button2 = Button(win2, text="Browse", command=lambda:browse(win2)).pack()
button1 = Button(root, text="New window", command=new_window).pack()
root.mainloop()

Tkinter error in pycharm : root is not defined

Hi I am a novice programmer.I am trying to use tkinter in pycharm....
When i try to use root or any other function it doesn't show any suggestion and gives not defined error.The program works fine in idle. But cann't seem to get it work with pycharm.Please help.I have already installed tkinter package and its also enabled in project interpreter settings....
here's what I am trying to do...
from tkinter import *
root = Tk()
topframe = Frame(root)
topframe.pack()
bottomframe = Frame(root)
bottomframe.pack(side = BOTTOM)
button1 = Button(topframe, text='Button 1', fg='red')
button2 = Button(topframe, text='Button 2', fg='blue')
button3 = Button(bottomframe, text='Button 3', fg='green')
button1.pack(side = LEFT)
button2.pack(side = LEFT)
button3.pack()
root.mainloop()
Just try this
from tkinter import Tk
root= Tk()
I also faced this problem before. This worked for me. I don't know the reason.
Note Tkinter has been renamed to tkinter in Python 3
tkinter — Python interface to Tcl/Tk.(Tk itself is not part of Python; it is maintained at ActiveState)
source : official Doc
Also there are certain classes that don't get imported when you try to import with *. When working with tkinter.
turn Around:
Solution 1.
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
root = tk.Tk()
Solution 2:check correct version is installed in pycharm.
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
root.mainloop()
Solution 3: Absolute Import
from tkinter import Tk
root= Tk()
w = Label(root, text="Hello, world!")
I had the same problem. I found that when I typed from tkinter there were 2 different options; you need to choose the option with a file icon next to it

How do I give focus to a python Tkinter text widget?

I'd like to be able to open the App GUI and have it automatically place the cursor into a particular text widget. Best case scenario is: as soon as the app is launched someone can start typing without having to click on the text widget. This is just a small example displaying the issue:
from Tkinter import *
root = Tk()
Window = Frame(root)
TextWidget = Text(Window)
TextWidget.pack()
Window.pack()
root.mainloop()
You use the focus_set method. For example:
from Tkinter import *
root = Tk()
Window = Frame(root)
TextWidget = Text(Window)
TextWidget.pack()
Window.pack()
TextWidget.focus_set()
root.mainloop()

Categories

Resources