I'm trying to make a basic window with the text "t" inside using Tkinter, however when running the code the shell spits out "NameError: name 'Label' is not defined". I'm running Python 3.5.2.
I followed the tutorials but the problem is in the label = Label(root, text="test") line.
import tkinter
root = tkinter.Tk()
sheight = root.winfo_screenheight()
swidth = root.winfo_screenwidth()
root.minsize(width=swidth, height=sheight)
root.maxsize(width=swidth, height=sheight)
label = Label(root, text="test")
label1.pack()
root = mainloop()
Is the label function different in 3.5.2?
You never imported the Label class. Try tkinter.Label
Check the import statements for those tutorials
Maybe they imply from tkinter import *
import tkinter
root = tkinter.Tk()
sheight = root.winfo_screenheight()
swidth = root.winfo_screenwidth()
root.minsize(width=swidth, height=sheight)
root.maxsize(width=swidth, height=sheight)
label = tkinter.Label(root, text="test")
label1.pack()
root = tkinter.mainloop() # <- prob need to fix this as well.
Because you didn't do from tkinter import * you need to invoke the Label from the tkinter module.
Alternatively you can do:
from tkinter import *
...
label = Label(root, text="test")
stumbled across the same Problem. Most beginners guides seem to mess up here.
I had to use a second line in the configuration:
!/usr/bin/python3
import tkinter
from tkinter import *
...
On Windows Operating System your code should run well but on macOS, you will get a problem. I don't know why something like that happen. Anyway try:
import tkinter,
from tkinter import*
And run
After that just write:
from tkinter import *
or
import tkinter
(not both this time)
It's a typo error...
Nothing to do with the import statements.
Label = with a capital L not l
Label(root, text="Username").place(x=20,y=20)
capitalize l
Related
i'm learning Python and i'm a beginner. To create a simple programm I use tkinter. With the Entry widget I try to input numbers in Phyton. Then i try to assign that input to a variable.
In the example below, the variable "hoehe" should take the value from the input.
My Phyton Version: (Phyton 3.10(64Bit))
Test Code:
from tkinter import *
import tkinter as tk
root = tk.Tk() # Phyton Fenster darstellen
root.title("Holzbinder")
root.geometry("200x100")
eingabefeld = Entry(root)
eingabefeld.pack()
def hoehe_ueber_null_greifen():
global hoehe_ueber_null
hoehe_ueber_null = eingabefeld.get()
return hoehe_ueber_null
hoehe = hoehe_ueber_null_greifen()
print(hoehe)
berechnungsknopf = Button(root, text="Berechnen", command=hoehe_ueber_null_greifen)
berechnungsknopf.pack()
root.mainloop()
I don't understand why it doesn't work. Can someone help me?
I tried win.overrideredirect(True) it didn't work...
My Code goes like this:
from tkinter import *
win = Tk()
win.resizable(0,0)
win.wm_protocol("WM_SAVE_YOURSELF", lambda: print("On exit"))
Label(win, text="Tk Window").pack()
win.mainloop()
Specs:
Python 3.9.6 [Latest],
Pip 21.1.3,
OS: Windows 10 Home
I want to make the minimize button to be disabled...
Please help
The thing #N1CK145 posted didn't work for me but I am guessing that he/she tried to do this:
import Tkinter as tk
root = tk.Tk()
root.attributes("-toolwindow", True)
root.mainloop()
If you want to know more attribute options look here
try resizeable function like this :
win= Tk()
win.geometry("750x250")
win.resizable(False, False)
Found this here:
import Tkinter as tk
root= tk.Tk()
root.title("wm min/max")
# this removes the maximize button
root.resizable(0,0)
# # if on MS Windows, this might do the trick,
# # but I wouldn't know:
# root.attributes(toolwindow=1)
# # for no window manager decorations at all:
# root.overrideredirect(1)
# # useful for something like a splash screen
root.mainloop()
hey beginner programmer here, making a to do list for a high school assignment and cant figure out this error. its also my first time using python as I usually use java and JavaScript for projects. Im trying to add a dark and light mode to my program. Am I doing this correctly? btw, i'm using the online ide repl.it
here's my code for reference
import tkinter
import tkinter.messagebox
from ttkthemes import ThemedStyle
import tkinter.ttk as ttk
import pickle
root = tkinter.Tk()
root.title("To-Do List")
def add_task():
task = entry_task.get()
if task != "":
listbox_tasks.insert(tkinter.END, task)
entry_task.delete(0, tkinter.END)
else:
tkinter.messagebox.showwarning(title="Warning", message="Please enter a task")
def delete_task():
try:
task_index = listbox_tasks.curselection()[0]
listbox_tasks.delete(task_index)
except:
tkinter.messagebox.showwarning(title="Warning", message="Please select a task first")
def load_tasks():
try:
tasks = pickle.load(open("tasks.dat", "rb"))
listbox_tasks.delete(0, tkinter.END)
for task in tasks:
listbox_tasks.insert(tkinter.END, task)
except:
tkinter.messagebox.showwarning(title="Warning", message="Cant find saved task file")
def save_tasks():
tasks = listbox_tasks.get(0, listbox_tasks.size())
pickle.dump(tasks, open("tasks.dat", "wb"))
# Dark and light modes
app = tk.Tk()
app.geometry("200x400")
app.title("Changing Themes")
# Setting Theme
style = ThemedStyle(app)
style.set_theme("scidgrey")
# Button Widgets
Def_Btn = tk.Button(app,text='Default Button')
Def_Btn.pack()
Themed_Btn = ttk.Button(app,text='Themed button')
Themed_Btn.pack()
# Scrollbar Widgets
Def_Scrollbar = tk.Scrollbar(app)
Def_Scrollbar.pack(side='right',fill='y')
Themed_Scrollbar = ttk.Scrollbar(app,orient='horizontal')
Themed_Scrollbar.pack(side='top',fill='x')
# Entry Widgets
Def_Entry = tk.Entry(app)
Def_Entry.pack()
Themed_Entry = ttk.Entry(app)
Themed_Entry.pack()
# Create GUI
frame_tasks = tkinter.Frame(root)
frame_tasks.pack()
listbox_tasks = tkinter.Listbox(frame_tasks, height=10, width=50)
listbox_tasks.pack(side=tkinter.LEFT)
scrollbar_tasks = tkinter.Scrollbar(frame_tasks)
scrollbar_tasks.pack(side=tkinter.RIGHT, fill=tkinter.Y)
listbox_tasks.config(yscrollcommand=scrollbar_tasks.set)
scrollbar_tasks.config(command=listbox_tasks.yview)
entry_task = tkinter.Entry(root, width=50)
entry_task.pack()
button_add_task = tkinter.Button(root, text="Add a task", width=48, command=add_task)
button_add_task.pack()
button_delete_task = tkinter.Button(root, text="Delete a task", width=48, command=delete_task)
button_delete_task.pack()
button_load_tasks = tkinter.Button(root, text="Load a task list", width=48, command=load_tasks)
button_load_tasks.pack()
button_save_tasks = tkinter.Button(root, text="Save your task list", width=48, command=save_tasks)
button_save_tasks.pack()
root = tkinter()
root.mainloop()
repl.it has nothing to do with the issue. You have lines referencing "tk", like app = tk.Tk(), but you never defined anything called tk. It looks like some of your code expects you to have imported Tkinter via import tkinter as tk, in which case tk would be valid. But you also have code expecting it to be called tkinter, like root = tkinter.Tk(). It seems like your code was inspired from multiple sources, some of which had Tkinter imported as tk, and some where it was imported as tkinter. All you have to do is replace all tks with tkinter. For example, this line:
Def_Btn = tk.Button(app,text='Default Button')
would become:
Def_Btn = tkinter.Button(app,text='Default Button')
The problem is probably coming from your import
I observed that you did not import Tkinter as tk but you keep on using the tk. try importing it
Since you have:
import tkinter
Python looks for tkinter not tk since you have not told python that you want to use the module with the alias 'tk'
Your solution is:
import tkinter as tk
You probably forgot that you were using tkinter instead of tk.
The problem is from your import statement, you imported tkinter as ttk but you kept on using tk
I tried to make a module in which I made a funtion which just reads and display the image in GUI. Then I made another module which makes call to that function when the button is clicked. Button gives me error.
#module code:
from tkinter import *
class disp:
def __init__(self):
root1.geometry("400x500")
image = PhotoImage(file = 'png2.png')
Label(root1,image=image).pack()
root1.mainloop()
#main code:
from tkinter import *
import testimg as ti
def click():
ti.disp()
root = Tk()
Button(text = 'Click me',command=click).pack()
root.mainloop()
In your class disp, you have put the master as root1 whereas in the main code, you have defined Tk() as root. This means that root1 is no window so the label that has a master of root1 has no where to pack itself.
You also need to remove root1.mainloop() because it’s useless and causing errors due to the fact that root1 doesn’t have Tk(). It’s like trying to loop a while statement without typing in while. This gives an error.
Below modified code is based on yours:
#module code:
from tkinter import *
class disp:
def __init__(self):
root1 = Tk()
root1.geometry("400x500")
image = PhotoImage(master=root1, file='png2.png') # set master to root1
Label(root1, image=image).pack()
root1.mainloop()
But using multiple Tk() instances is not a good design.
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