python 3.3 tkinter-- "window' is not defined' - python-3.x

i am a newbie # python GUI's so i am using an online tutorial. when i run the following code, i get an error saying 'window' is not defined. i am using a windows PC with windows 7 and python 3.3 installed.
from tkinter import *
window.title("Test Window")
window.geometry('300x300')
window.wm_iconbitmap('Generals.ico') # Generals.ico is a filename for the window icon
lbl = tkinter.Label(window, text='Label')
lbl.pack()
window.mainloop()

your missing one line. You need to add window = Tk()
so your code should look like:
from tkinter import *
window = Tk()
window.title("Test Window")
window.geometry('300x300')
window.wm_iconbitmap('Generals.ico') # Generals.ico is a filename for the window icon
lbl = tkinter.Label(window, text='Label')
lbl.pack()
window.mainloop()
Not sure if this is 100% correct cause I'm using python 2.7. You might not need to capitalive the 'T' in tk()

Related

Tkinter window stopped updating

I just started working on Tkinter with help of youtube. I tried to show Label in root window it showed, then button, it didnt work. Now it is not showing anything and root window is coming as empty whatever I do, that too in a fixed dimension.
from tkinter import *
root1 = Tk()
questionLabel = Label(root1, text="Hello World", fg="red")
questionLabel.pack
# submitButton = Button(root, text="Submit", fg="blue")
# submitButton.pack
root1.mainloop()
Python version: 3.9
VS Code version: 1.68.1

tkinter gui window is not opening but the code is running with exit code 0

from tkinter import *
def main_account_screen():
main_screen = Tk()
main_screen.geometry("300x250")
main_screen.title("Account Login")
Label(text="Choose Login Or Register", bg="blue", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
Button(text="Login", height="2", width="30").pack()
Label(text="").pack()
Button(text="Register", height="2", width="30").pack()
main_screen.mainloop()
main_account_screen()
tk.mainloop()
I'm not getting any GUI when I run the .py file. Can anyone help me why this doesn't work. I'm using Pycharm but I have also tried running the program with the regular python interpreter.

How to fix this syntax error with the while loop

I am trying to make it so that when you close the tinker GUI another one opens and this process repeats
I have tried this while command but it says invalid syntax.
from tkinter import *
root = Tk()
photo = PhotoImage(file="scary.png")
label = Label(root, image=photo)
label.pack()
root.mainloop()
while 2 > 1
You can try this for infinite times opening the tkinter window:
from tkinter import *
from PIL import Image, ImageTk
image = Image.open("scary.png")
photo = ImageTk.PhotoImage(image)
while True:
root = Tk()
label = Label(root, image=photo)
label.pack()
root.mainloop()
EDIT
I've added code for opening image of any format in Tkinter, for that you have to install the required package with pip install pillow (PIL package)
I've tested it without image, it is working. Hope it helps !
You were missing the loop body.
Now it has but since 2 > 1 is always true it's an infinite loop.
from tkinter import *
root = Tk()
photo = PhotoImage(file="scary.png")
label = Label(root, image=photo)
label.pack()
root.mainloop()
while 2 > 1:
pass

okay so I created this code to create a GUI but i need to add buttons and when ever i try it creates a new window. what should I do?

this is my code so far o cant add buttons with out it creating more windows
////////
#import tkinter
import tkinter
#import tkmessagebox(buttons)
from tkinter import *
#create a new window
window = tkinter.Tk()
#title <------ put it before .mainloop
window.title("yeahh boiiii")
#window size
window.geometry("500x500")
#set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico')#<---- 8bit file name
master = Tk()
def callback():
print ("click!")
b = Button(master, text="OK", command=callback)
b.pack()
#draws the window
window.mainloop()
////////
please help
Your problem is that you create 2 instances of Tk(). This is a bad idea, and you don't need to do it since you can make your button a child of the window object:
# Import tkinter
import tkinter as tk
# Create a new window
window = tk.Tk()
# Title <------ put it before .mainloop
window.title("yeahh boiiii")
# Window size
window.geometry("500x500")
# Set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico') #<---- 8bit file name
def callback():
print ("click!")
b = tk.Button(window, text="OK", command=callback)
b.pack()
# Draw the window
window.mainloop()
I also rewrote your tkinter import, because you were importing it twice...

Why is tkinter not working in visual studio?

I'm working on a new project in visual studio as shown in the code below, and the GUI using Tkinter is not working in visual studio. This is my first time using visual studio and I can't seem to find why it won't work.
from tkinter import *
import tkinter as ttk
#import os #not needed
root = Tk()
#Setting up the window
root.geometry("250x100")
root.resizable(width=False, height=False)#Disables user to resize window
root.title("Login")
#Temp "DataBase"
users=[("josh","python")] #<<<here ''josh'' is user and ''python'' i5s password
admins=[("josh1","python1")]
# Login and signup function
def login(): #login function
if (t1.get(),t2.get())in users: #Temp for testing
root.destroy()
import MainWindow
# os.system("MainWindow") #does not work
print("welcome")
elif (t1.get(),t2.get())in admins: #Temp for testing
root.destroy()
import AdminMainWindow
# os.system("AdminMainWindow") #does not work
print("welcome Admin")
else:
error.config(text="Invalid username or password")
def signup(): #signup function
root.destroy
import SignupWindow
# os.system("SignupWindow") #does not work
#arranging display varables
top = Frame(root)
bottom = Frame(root)
top.pack(side=TOP, fill=BOTH, expand=True)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
#error placement and font
error = Label(root, font=("blod",10))
error.place(x=40,y=55)
#input display setup
l1 = Label(root,text="Username:")
l2 = Label(root,text="Password:")
t1 = Entry(root, textvariable=StringVar())
t2 = Entry(root, show="*", textvariable=StringVar())
b1 = Button(root,text="Login", command=login)
b2 = Button(root,text="Signup", command=signup)
#organising
l1.pack(in_=top, side=LEFT)
t1.pack(in_=top, side=LEFT)
l2.pack(side=LEFT,)
t2.pack(side=LEFT,)
b1.pack(in_=top, side=BOTTOM)
b2.pack(in_=bottom, side=BOTTOM)
#end of Tk loop
root.mainloop()
It comes up with the python command line and says press any key to continue.
I also looked online and they all say it because people don't end the Tk loop, but I have.
before you make a new project I created a new file and places all the code in there. then add one code to VS at a time then it works but not when you do it all together.
On ms-windows, python programs using tkinter should have the extension .pyw. And this extension should be associated with pythonw.exe rather than python.exe.
Using pythonw.exe will prevent the cmd.exe window from appearing when your python script has a GUI.

Resources