Tkinter new window declaration - python

Recently while using python tkinters declaration has not been working. What i mean by this is that a for any file where tkinter has not been imported before, simple code such as creating a window is not possible. Has anyone else encountered this issue? If so, how is it solved?
Any feedback is appreciated. Thanks.
The code is simply supposed to open a window in tkinter. But, when run, no window is displayed.
from tkinter import *
def sample():
window = Tk()
sample()

That's so interesting.
You have to use tkinter.Tk() instead of Tk().
Thanks

Try This :
from tkinter import * # Importing everything from the Tkinter module
win = Tk() # Initializing Tkinter and making a window
def sample():
new_win = Toplevel() # This creates a new window
sample()
win.mainloop() # If this is not there, the code won't work

Related

Unable to see tkinter GUI in VS however the code works other editors

Here's the code
from tkinter import*
a= Tk()
a.title("test")
a mainloop
It doesn't give an error just says press any key to continue
The problem is the a mainloop part. It should be a.mainloop() instead.
Updated code:
from tkinter import *
a = Tk()
a.title("test")
a.mainloop()
Hope this helps!

while using Pyautogui in Tkinter for automtion, window gets resized and misplaced

I have programmed a tkinter window for virtual assistant and I'm using pyautogui.
Following is the simplest code I am using to take ss after the func in called from button click in a tkinter window.
This program take the screenshot but resizes(smaller) and misplaces the window from corner of the desktop to the center.
Can any one help me with this why is it happning.
I have tried the if _ name_ == '_ main _' for the tkinter file(main.py) and init method for pyautogyi file(screen_shot.py)
and i can't keep both of the in same file.
The same thing happens when i try to open webbrowser and use pyautogui to click on the browser window.
me tkinter
plz tell me what i am doing wrong or any solution to it.
thanks
def takescreenshot():
pyautogui.hotkey('win','printscreen')
return "done"
Yes, You can stop tkinter to stop resizing by including following lines of code
import tkinter as tk
root = tk.Tk()
root.resizable(width=False, height=False)
root.mainloop()
If it dont works please let me know!

Tkinter .pack() window not showing?

I've been working on a simple program that make a button output something. But when i run it,
this
(I got this from the internet btw) does not show up. Is somethoing wrong with the code or something?
Please help me so the window above can appear :)
Code:
from Tkinter import *
def asdf():
print('test')
tk = Tk()
b = Button(tk, text="test", command=asdf)
b.pack()
You forgot to call the Tk.mainloop method at the end of your program:
from Tkinter import *
def asdf():
print('test')
tk = Tk()
b = Button(tk, text="test", command=asdf)
b.pack()
##############
tk.mainloop()
##############
Doing so starts Tkinter's main event loop and creates the window.
It seems you are using Python3, as there are parentheses after print, so from Tkinter import * should be from tkinter import *. Python is case-sensitive. You also forgot to call root.mainloop() at the end of your code as #user2555451 mentioned, although a window should appear all the same, but stop responding when any event occurs (e.g., clicks, key presses, focus changes).

Tkinter pyimage doesn't exist

I know there are lot of similar questions, but there aren't any simple enough that I am able to understand. I have the following code:
import Tkinter as tk
from PIL import Image, ImageTk
class MainWindow:
def __init__(self, master):
canvas = Canvas(master)
canvas.pack()
self.pimage = Image.open(filename)
self.cimage = ImageTk.PhotoImage(self.pimage)
self.image = canvas.create_image(0,0,image=self.cimage)
filename = full_filename
root = tk.Tk()
x = MainWindow(root)
mainloop()
and I get the following error:
TclError: image "pyimage36" doesn't exist
I've read some stuff about the image objects getting garbage cleaned but I don't quite understand it.
Figured it out. For some reason, while running in the debugger, if any previous executions had thrown errors I get the "pyimage doesn't exist" error. However, if I restart the debugger (or no previously executed scripts have thrown errors), then the program runs fine.
I had the same error message when using spyder 3.3.6 the only way i could get the .png file to load and display after getting the 'Tinker pyimage error ' was to go to the Console and restart the kernel. After that i worked fine.
(Python 3.8)
If you are using a IDE with a console(such as Spyder) just call root.mainloop() in the console.
Odds are that you have a bunch of partially loaded tkinter GUI's that never managed to be executed due to an error that prevented the root.mainloop() function from being run.
Once you have run the root.mainloop() a bunch of GUI's will likely appear on screen. After you have closed all those GUI's try running your code again.
Image of multiple Tkinter GUI's appearing on screen
Read more about mainloop() here: https://pythonguides.com/python-tkinter-mainloop/
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
img = Image.open('Paste the directory path')
bg = ImageTk.PhotoImage(img)
lbl = Label(root, image=bg)
lbl.place(x=0, y=0)
mainloop()
I was getting the same error. Try this code this will help you.
Additionally, in case if you create a button and use it to open other window, then there use window = Toplevel(), otherwise it will again show the same error.
From programmersought
image “pyimage1” doesn’t exist
Because there can only be one root window in a program, that is, only one Tk() can exist, other windows can only exist in the form of a top-level window (Toplevel()).
Original code
import tkinter as tk
window = tk.TK()
Revised code
import tkinter as tk
window = tk.Toplevel()
Keep other code unchanged
https://www.programmersought.com/article/87961175215/

How to show a window that was hidden using "withdraw" method?

I would like to show a window after I called withdraw.
The following is my current code:
from Tkinter import *
def callback():
global root
root.withdraw()
win2 = Tk()
root = Tk()
Label(root,text='this is a window').pack()
Button(root,text='withdraw',command=self.callback).pack()
mainloop()
As soon as I press the button, the window disappears much as I want it, and another window appears and everything works great. How do I get the first window back, in the same state as it was before?
Use the following commands when you want to show the window:
# root.update() # not required
root.deiconify()
If you want to know more about it, see here.

Categories