(Python - Tkinter) During a function, using an image as second window background - python

So I've looked around at forums and video tutorials but I can't seem to solve the problem I'm having. Basically, I'm creating a GUI with multiple windows. When trying to set up my second window, I attempt to set an image as the background using the following code;
def openMusicPage(): 1
MusicPage = Tk() 2
MusicPage.title('Now This Is Epic: Top Music Chart') 3
MusicPage.configure(width = 800, height = 505) 4
# Background`enter code here`
MusicBackground = PhotoImage(file="MusicBackground.gif") 5
MusicBackground_label = Label(MusicPage, image = MusicBackground) 6
MusicBackground_label.image=MusicBackground 7
MusicBackground_label.place(x=0, y=0) 8
The error occurs on the 6th line saying the image doesn't exist. On other forums and videos, I've seen suggestions to add the 7th line to fix this issue by referencing the image again. However, this hasn't helped and I continue to receive the error. Any suggestions as to how I could solve this issue? Noting, I am only able to use Tkinter for this assignment.
Thanks in advance!

Only use one instance of Tk(). For additional windows use Toplevel(). Change that and it works just fine.

Related

Tkinter Label doesn't work as I thought it does [duplicate]

This question already has an answer here:
When should I use root.update() in tkInter for python
(1 answer)
Closed 1 year ago.
Beginner programmer here currently trying to learn Tkinter for a school assignment.
I have a GUI class that stores the Tkinter labels etc, the labels are innitiated like this:
# GUI for Player 1
self.player_1_name_field = Label(
self.root,
text="Player 1",
font=GUI_Settings.player_information_font,
anchor=W,
background=GUI_Settings.playerfield_active_color
)
I then create a Game() object that looks like this:
class Game():
def __init__(self):
self.GUI = GUI()
self.GUI.initializeBoard()
self.GUI.root.mainloop()
When I run the code, the labels do get created and are where they are supposed to be, but are completely black. Once I move or resize the window it instantly becomes how I want it to be, it just behaves weird when at the start of the code
The interesting thing is that I also have a Canvas and a List that work perfectly fine, only the Labels are not cooperative
If you need further info, just ask for it!
Thank you!
Edit 1: I have a function called drawWindow() that redraws the chessboard when I re-configure the window. In the init of the GUI class I set self.root.bind("<Configure>", self.drawWindow). If I remove that line of code, the Labels work but the Canvas doesn't anymore. I'm so confused. For anyone wanting to take a look at my tiny code: https://codeshare.io/DZYzyZ
See comment of Thingamabobs
The issue is self.root.update(). Remove this line and you'll be fine.
When should I use root.update() in tkInter for python.
This works but you shouldn't do it
This is a tricky issue. Your problem come from the bind of the configure event. Bind to the root window, it is applied to all sub-widgets of the window, which cause the bug (I don't know why yet).
This will solve your issue (line 202):
self.chessboard.bind("<Configure>", self.drawWindow)
instead of:
self.root.bind("<Configure>", self.drawWindow)
Result without moving or resizing the window:
I found the information here (french forum).

my button in tkinter is not showing the image [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 2 years ago.
this code is just an example
code:
import tkinter as tk
root = tk.Tk()
photoImageObj = tk.PhotoImage(file="signout.png")
lab = tk.Label(root, image=photoImageObj).pack()
photoImageObjj = tk.PhotoImage(file="signout.png")
signout_button=tk.Button(root,image=photoImageObjj).pack()
root.mainloop()
In this code it is working and output is also working fine.
here is the output:
but in my real project, where I am using this button with the image is not working.
code:
photoImageObj = PhotoImage(file="signout.png")
signout_button=Button(stem,image=photoImageObj).pack()
here I haven't pasted my whole code since it is having 50-60 line's of code.
Output:
so if the same code is working fine, then there is problem in my real project file.
so can anyone help me debug that.
When an image is inside a function you have to keep reference to the image or it will be garbage collected by python. With the code provided its hard to say, but just try something like:
signout_button = Button(stem,image=photoImageObj)
signout_button.pack() #so that signout_button is not None
signout_button.image=photoImageObj #keeping a reference
Alternatively, you could also say global photoImageObj on top of function, but its not recommended to use global. This question should be marked as a duplicate and closed, but just in-case you dont understand what to do in your "specific case", here is the answer.

How do I make a 800x800 console window with python

I've recently been making a simple text based game in python 3.9 and I want to make it so when you run the .exe it will open a console window that is 800x800 or whatever ratio I want to put in. Could anyone help me do this please!
The only option that i can think of its to run tour program in PowerShell, this one can resize it self with something like this
$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = 3000
$newsize.width = 150
$pswindow.buffersize = $newsize
$newsize = $pswindow.windowsize
$newsize.height = 50
$newsize.width = 150
$pswindow.windowsize = $newsize
I get this code from here
You can call this commands inside python with OS module.
I couldnt test the code cause #teamLinux.

Python: Tkinter: How to change the windows border color?

Every result I've gotten from searching this question up has to do with changing the border color for Tkinter widget's WITHIN the main app window, or changing the background color and stuff.
What I'm looking to do is actually change the windows border color itself. Which I have marked in this picture here
I read some stuff concerning 'Styles' but it didn't seem to be what I was looking for.
Can anyone help point me to a tutorial for changing this, or explain how it can be done?
Or if there is a question that asked exactly the same question I was asking that is answered point me to it.
As I see you are using windows.
This color is set by the theme you are currently using. It is the same for every window.
So I cross out the possibility of only using the Tkinter module for this.
Tkinter is responsible for what is in the window but the window manager decides about the border. For example in Ubuntu the window would look totally different.
I guess, you would need some windows specific calls for that.
You can remove the border with root.overrideredirect(1) if I remember correctly.
PS: put "windows" into the tags of this question.
Hi I was looking for this answer too, found it after like 80 minutes searching, Only work, to activate DWMWA_USE_IMMERSIVE_DARK_MODE
found it here: Can I change the title bar in Tkinter?
I didnt found dwmwindowattribute in dwmwindowattribute that affect Top bar color sadly :(.
For border colour:
It should be possible to call DWMWA_BORDER_COLOR, but honestly I dont know how, there is some article calling it in C++ here:
change-the-color-of-the-title-bar-caption-of-a-win32-application
Tried this but doesnt work:
set_window_attribute(hwnd, 22, '0x000000FF', 4)
Here is working code for pure black top:
import tkinter as tk
import ctypes as ct
def dark_title_bar(window):
"""
MORE INFO:
https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
"""
window.update()
set_window_attribute = ct.windll.dwmapi.DwmSetWindowAttribute
get_parent = ct.windll.user32.GetParent
hwnd = get_parent(window.winfo_id())
value = 2
value = ct.c_int(value)
set_window_attribute(hwnd, 20, ct.byref(value),
4)
root = tk.Tk()
root.title("Crystaly Ball")
root.geometry("1400x900")
root.configure(background="#222246")
dark_title_bar(root)
root.mainloop()

Tkinter Canvas Flicker

I'm drawing small black and white video frames to a Tkinter canvas using this code (at 10Hz)
self.image.buf = bytearray(header.width * header.height);
self.image.buf[:] = image
self.image.im = Image.frombuffer("L", (header.width, header.height), self.image.buf).resize((320, 240)).transpose(Image.ROTATE_180)
self.image.tkimage = ImageTk.PhotoImage(self.image.im)
if (self.image.id): self.image.delete(self.image.id);
self.image.id = self.image.create_image((0, 0), image=self.image.tkimage, anchor=NW)
Everytime a frame gets drawn, the widget flickers. Isn't the Tk canvas supposed to be double buffered? what can I do to avoid this?
So i figured out the problem -- it seems you have to create your tkimage from the same thread that tk is running in or bad things happen. Thanks to anyone who looked at this!
I don't think there's enough detail in your question to say for certain what the problem is. It's possible to swap images in and out without flicker and your code doesn't look too unusual, so there may be something else in your code that is causing the problem.
Here's one thing to try: instead of deleting and re-creating the canvas item each iteration, try using one canvas item that you reconfigure to use the new image using the itemconfig method.
For example:
if self.image.id is None:
self.image.id = self.image.create_image(...)
else:
self.image.itemconfig(self.image.id, image=self.image.tkimage)
Also, if you're not using the canvas for anything else you might want to consider using a label widget rather than a canvas and an image item.

Categories

Resources