Tkinter window stopped updating - python-3.x

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

Related

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

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.

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...

python 3.3 tkinter-- "window' is not defined'

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

Resources