Tkinter window not filling screen in zoomed state on secondary display - python

I'm building a small python 3 tkinter program on windows (10). From a main window (root) I'm creating a secondary window (wdowViewer). I want it to be full screen (zoomed) on my secondary display. The code below works on my main setup with two identical screens. If I however take my laptop out of the dock and connect it to (any) external display, the new window only fills about 2/3 of the secondary display.
Two things to note:
- The laptop and external monitor have same resolution.
- The window is appropriately zoomed when overrideredirect is set to 0.
mon_primary_width = str(app.root.winfo_screenwidth()) # width of primary screen
self.wdowViewer = Toplevel(app.root) # create new window
self.wdowViewer.geometry('10x10+' + mon_primary_width + '+0') # move it to the secondary screen
self.wdowViewer.wm_state('zoomed') # full screen
self.wdowViewer.overrideredirect(1) # remove tool bar
app.root.update_idletasks() # Apply changes

After two days of experimenting I finally found a fix.
Consider the following example: Making a toplevel zoomed, works fine on any secondary display when using the following code:
from tkinter import *
# Make tkinter window
root = Tk()
sw = str(root.winfo_screenwidth())
Label(root, text="Hello Main Display").pack()
# Make a new toplevel
w = Toplevel(root)
w.geometry("0x0+" + sw + "+0")
w.state('zoomed')
w.overrideredirect(1)
Label(w, text='Hello Secondary Display').pack()
root.mainloop()
However, in my code I'm making a new Toplevel after running the mainloop command. Then, the issue arises. A code example with the issue:
from tkinter import *
# New Tkinter
root = Tk()
sw = str(root.winfo_screenwidth())
# Function for new toplevel
def new_wdow():
w = Toplevel(root)
w.geometry("0x0+" + sw + "+0")
w.state('zoomed')
w.overrideredirect(1)
Label(w, text='Hello Secondary Display').pack()
# Make button in main window
Button(root, text="Hello", command=new_wdow).pack()
root.mainloop()
Problem: The bug is only present if the DPI scaling in Windows is not set to 100%. Hence, there seems to be a bug in how tkinter handles the DPI scaling subsequent to running mainloop. It does not scale the window properly, but Windows is treating it as if it does.
Fix: Tell Windows that the app takes care of DPI scaling all by itself, and to not resize the window. This is achievable using ctypes. Put the following code early in your python script:
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(2)
Hopefully others will find the solution helpful. Hopefully, someone may explain more of what is going on here!

Related

How to make window look like a win10 widget. Python

I'm trying to create a windows app that shows weather like windows 10 widgets using Tkinter. The issue I ran into is that I cannot remove the window's border to make it look like a real widget.
Here's what it looks like and what I'd like to remove:
self.root.overrideredirect(1) kinda works but I still can minimize the window, which is what I do not want to be available.
That's what I want it to look and behave like:
To sum up:
How to disable window title and all of that on top properly?
How to move the window after, set the position of it on the screen?
I'm new to Python, any help is appreciated. Thank you all. Peace
To clarify: The app is supposed to be not affected by tab+alt (and other ways to minimize it) and be always on desktop, not on top of any other app, right on the desktop.
UPD
The thing I missed working around with win32gui is that I cannot get the handle the way I was trying to due to the fact that the code of getting the handle before the window itself was created (before mainloop) so root.wait_visibility() was the solution for me. After that I got the handle using EnumWindows and positioned my window the way I needed, finally disabling the border with root.overrideredirect. The suggested solutions will be tried as well. Thank you for help!
top-right corner of the desktop:
You can try this:
root.overrideredirect(1)
root.geometry('250x150+900+600')
#900+600 is x+y axis position of window
And for making close button like of widget you can make transparent frame in side, put button there and in command .destroy().
Edit
From TheLizzard's comment, visit here for further information. From there:
To make the window draggable, put bindings for (mouse clicks) and (mouse movements) on the window.
import tkinter
class Win(tkinter.Tk):
def __init__(self,master=None):
tkinter.Tk.__init__(self,master)
self.overrideredirect(True)
self._offsetx = 0
self._offsety = 0
self.bind('<Button-1>',self.clickwin)
self.bind('<B1-Motion>',self.dragwin)
def dragwin(self,event):
x = self.winfo_pointerx() - self._offsetx
y = self.winfo_pointery() - self._offsety
self.geometry('+{x}+{y}'.format(x=x,y=y))
def clickwin(self,event):
self._offsetx = event.x
self._offsety = event.y
win = Win()
win.mainloop()
You can use this for some part in top of the window rather then in whole window. By making new frame there implement it, to make your widget moveable.
And from second comment of TheLizzard, visit here for more information. From there:
If you want the window to stay above all other windows.
root.attributes("-topmost", True)

How do I change the size of a tkinter canvas through a new window?

So I have one Tkinter screen that has a canvas. I want to change the size of the canvas by creating a new window that has entry widgets. So I created a new screen and added 2 entry widgets. I want to get the value from those widgets and based on that...it should change the size of the canvas. I tried to do this for an hour, but no luck. Please assist me.
Here is my code
from tkinter import *
# create root window
root = Tk()
# Create Canvas
canvas = Canvas(root, width=50, height=50)
# Create an additional window (the one that is used to enter the new geometry)
dialog = Toplevel(root)
# Add entry widgets for width and height to the new window
width_entry = tk.Entry(dialog)
height_entry = tk.Entry(dialog)
# Add a button to the new window that applies the given width and height
apply_button = Button(dialog, text = 'Apply geometry', command = lambda: canvas.geometry(width_entry.get()+'x'+height_entry.get()))
# Its not possible to get the geometry of a canvas in tkinter...so how do I change the size.
# display the entry boxes and button
width_entry.pack()
height_entry.pack()
apply_button.pack()
# start the tk mainloop
root.mainloop()
Please Assist me
The command you are looking for is canvas.config
Here, I have adjusted the given code:
import tkinter as tk
# create root window
root = tk.Tk()
# Create Canvas
canvas = tk.Canvas(root, width=50, height=50)
canvas.pack()
# Create an additional window (the one that is used to enter the new geometry)
dialog = tk.Toplevel(root)
# Add entry widgets for width and height to the new window
width_entry = tk.Entry(dialog)
height_entry = tk.Entry(dialog)
# Add a button to the new window that applies the given width and height
apply_button = tk.Button(dialog, text = 'Apply geometry', command = lambda: canvas.config(width=width_entry.get(), height=height_entry.get()))
# display the entry boxes and button
width_entry.pack()
height_entry.pack()
apply_button.pack()
# start the tk mainloop
root.mainloop()
I also changed a couple other things:
You imported * from tkinter, but for some items you still led with tk.; I changed them all to match that and switched the import to match as well. (You could still use *, but then just don't have the leading tk.s.)
The canvas was never packed so you could never see what was going on there.
One more suggestion, that line where you make the button is really long. Maybe make a function that does what the lambda does and assign its command to that function instead of a lambda. You can probably see that a line that long is even hard to read here much less if someone (maybe a future version of yourself) was to try to read your code, and edit it or make sense of it. Generally, try to keep all lines down to 80 characters.
Let us know if you have any more questions etc.

Tkinter: Is there any way to remove the top bar of my program?

Okay, so i am using tkinter to make a GUI, i feel like the top bar looks kinda ugly so that's why i was planning to remove it and put some nice icons. I used this line of code to remove the top bar gui.overrideredirect(True) and it worked :D, but you can't move the program around the screen (it sticks to the top-left corner of the screen). Is there any other way i can do this without sticking my program to the corner? Thanks.
import requests
import tkinter as tk
#GUI Config
gui = tk.Tk()
gui.geometry('970x569')
gui.title("Zombs Royale Tools v1")
gui.resizable(False, False)
#Code below is the one i used to do this[![enter image description here][1]][1]
gui.overrideredirect(True)
#Background-image
bgImage = tk.PhotoImage(file="width.png")
background_label = tk.Label(gui, image = bgImage)
background_label.place(x=0,y=0,relwidth=1, relheight=1)
#GUI Widgets#
#Exit Button
exit_image = tk.PhotoImage(file="close.gif")
exit_button = tk.Button(gui, image=exit_image, borderwidth=0, command=gui.destroy)
exit_button.place(rely=0.01, relx=0.01)
Removing the titlebar is done with override redirect. However, this removes every window function as well. That includes moving the window around and resizing it.
This means with overrideredirect you can skip the
gui.resizable(False, False)
One thing you can do is provide more info for the window’s geometry:
gui.geometry('970x569+600+500')
The 2 last numbers provide the info regarding the positioning of your window on the desktop, in case you don’t want it to be spawned on the top left corner.
If you want to make the window movable you will have to integrate your own code. It has been discussed here before.
Eg.
here

Tkinter: disable text widget content auto resize (newline), when user resize window

Advice: i'm working on Windows 8.1, with Python and Tkinter at last version; i wrote from tkinter import * to post less code here, i know that it is a bad practice, use for example import tkinter as tk instead.
I'm coding my first text editor, and i have a problem when i resize the main windows. When i run the program it display on screen a window with dimension 750x500 pixel. So far, all ok, i can write text without problem (note that menu_bar and other features are work-in progress, but we dont care about them). Problem is with Text widget when user tries to resize window with cursor. The content of the text practically adapts to the size of the window (the length of each string is reduced or increased based on the width of the window). But i don't want that this happen. I want that Text widget changes his width automatically in base of window size, but the content mustn't be adapted. I hope that you understand my question, if not, i will try to explain better.
I have searched on online reference if there's a parameter to set this option, but i haven't found anything.
How to solve the problems concerning the Text widget and resizing the window?
from tkinter import *
root = Tk()
root.geometry("750x500")
content_text = Text(root, wrap=WORD, bg="grey25", undo=True, cursor="",
insertbackground="red", foreground="white", font="courier 12")
content_text.pack(fill=BOTH, expand=True)
scroll_bar = Scrollbar(content_text)
content_text.configure(yscrollcommand=scroll_bar.set, selectbackground="dodgerblue")
scroll_bar.configure(command=content_text.yview)
scroll_bar.pack(side=RIGHT, fill=Y)
if __name__ == '__main__':
root.mainloop()
You can't do what you want. If you have wrapping turned on, text will always wrap at the edge of the window. When you change the width of the window, the text will re-wrap to the new width. There is no configuration option to tell the widget to wrap at any other place.

Python tkSimpleDialog Fullscreen and Focus Over Fullscreen Parent

I am having a problem with getting tkSimpleDialog to take focus over my fullscreen window GUI. I have a GUI that I am trying to use a dialog window to use as an admin password to close the GUI (like a kiosk mode app) using root.quit(). The problems with this are that the dialog does not come in front of the parent windows if the parent windows are fullscreen. Additionally, I would like to make the tkSimpleDialog go fullscreen as well.
Here is the code for how the dialog box is being called/created using the tkSimpleDialog.py in my program:
def check_admin_password():
# use askstring here to verify password.
pass_attempt = simpledialog.askstring("Verifying access","Please enter Admin password", parent=window, show="*")
if pass_attempt == "password":
root.quit() # used whatever your instance of Tk() is here in place of root.
admin_minimize_button = Button(window, text = "Administrator", command = check_admin_password, width=35, height=12)
admin_minimize_button.grid(row=4, column=0)
I am using the following code for the parent window and I believe there is something with the overrideredirect(True) that is affecting the focus of my dialog window:
qwindow = Toplevel(root) #renames Toplevel "popup" window frame to variable "qwindow"
qwindow.title('Mission Queue') #creates title on popup window
w, h = qwindow.winfo_screenwidth(), qwindow.winfo_screenheight() #aquires dimensions from display size
qwindow.geometry("%dx%d+0+0" % (w, h)) #sets window size to aquired dimensions
qwindow.overrideredirect(True) #removes top bar and exit button from parent window frame
I have tried editing the tkSimpleDialog.py file and adding an overrideredirect(True) line however that is not working. If more information is needed, let me know. Any advice would be much appreciated. Thanks in advance.
Due to my inability to figure out a solution using tkSimpleDialog, I attempted a different approach creating my own "dialog". Unfortunately, I was still unable to get the dialog to have the entry line take focus with the screen window being set to wm_attributes('-fullscreen', 'True') and overrideredirect(True). While it would have been better to have both work; for my purposes, it will work fine to just be fullscreen in my application without overrideredirect(True). For anyone with a similar issue or wanting to see more documentation on my progress, heres the link to my other forum post: (Tkinter Entry Widget with Overrideredirect and Fullscreen).
Thanks for the help!

Categories