The boss said I don't understand tkinter: python tkinter window size position center

The following content is based on python3.6, tk8.5, Windows10, 1920x1080, no external screen.
There may be differences in different environments. If you have test data, I hope you can remind the blogger~

Preface:
        The thing is like this, the boss asked me to use python tkinter to make an interface, and put the window in the center of the screen. I saw that it was 1000% (屏幕宽度-窗口宽度)/2complete , but the boss said that I was wrong...
No sir

1. Drawing of tkinter window

  1. Create tkinter window

    import tkinter as tk
    window = tk.Tk()
    
    window.mainloop()
    
  2. window drawing
    Tk

    As shown in the figure, when tkinter generates a window, it will draw a border with a size of 1 pixel, a title bar with a height of 30 pixels, and an inner page of width×height pixels.

2. Define the window position

The function used to define the window position iswindow.geometry(‘width x height ±x ±y’);
You can also define the size separately.geometry(‘width x height’)or location.geometry(‘±x ±y’);

⚠️ Precautions:

  1. width x heightThe xis a lowercase letter x, do not write it as a multiplication operator ×;
  2. x yIt is based on the pixel points on the four corners of the window (excluding borders) as the coordinate origin;
  3. +xIndicates the distance between the left border of the window and the left border of the screen;
  4. -xIndicates the distance between the right border of the window and the right border of the screen;
  5. +yIndicates the distance between the upper boundary of the window title bar
  6. -yIndicates the distance between the lower boundary of the window and the lower boundary of the screen;
  7. x yitself can also be negative;
  8. x yA value of 0does not mean that it coincides with the corners of the screen, but leaves a gap to display borders and shadows, as shown in the following illustration:
++ upper left +-bottom left -+upper right --bottom right

So, if you want to align the window to the upper left corner of the screen, you can write:

window.geometry('500x300+-7+0')

Immediately above the right corner:

window.geometry('500x300--7+0')

Immediately lower left:

window.geometry('500x300+-7--7')

Immediately lower right:

window.geometry('500x300--7--7')

⚠️Precautions:
If your computer is set with Windows display scaling, assuming it is set to 125%, then the above 7should changed to 8×1.25-1=9;

3. Window Screen Centering Algorithm

  1. Get screen resolution

    '''主屏参数'''
    scr_width = window.winfo_screenwidth()	# 屏幕宽度
    scr_height = window.winfo_screenheight()	# 屏幕高度
    
  2. Calculate the positioning of the window when it is centered
    According to the second section, analyze the position of the window when it is centered up, down, left, and right (assuming the window size is 500x300):

    # 窗口和屏幕参数
    width = 500
    height = 300
    scr_width = 1920
    scr_height = 1080
    # 计算左上角位置
    x = (scr_width-width-16)//2
    y = (scr_height-height-32)//2
    # 放置窗口
    window.geometry(f'{
            
            width}x{
            
            height}+{
            
            x}+{
            
            y}')
    '''此算法得到的 x 可用于 +x 和 -x
    得到的 y 用于 +y,若用于 -y 需要将 y 再减 7
    
    此外,若HDPI屏幕缩放率为 scale_rate != 1.00,x y 也无需进行修正;
    只是 y 用于 -y 时需要修正 y = int(y-8*scale_rate+1)
    '''
    
    window_screen

    Window centering function:

    def set_window_center(window,width=None,height=None):
    	'''窗口屏幕居中算法
    
    	坐标原点: 窗口四个角(边框不算)
    	窗口绘制: 左下右各与屏幕间距8像素,其中1像素绘制边框;上与屏幕间距1像素绘制边框;间距用于边框与阴影
    	屏幕缩放: 窗口(大小x缩放倍率),左下右与屏幕间距(8x缩放倍率)像素,其中1像素绘制边框,即边框不参与缩放
    	算法精度: 误差1像素(窗口大小为奇数或屏幕有缩放时)
    	屏幕缩放: 自适应缩放率
    	'''
    	if width==None:width=200
    	if height==None:height=200
    	scr_width = window.winfo_screenwidth()
    	scr_height = window.winfo_screenheight()
    	size_position = f'{
            
            width}x{
            
            height}+{
            
            (scr_width-width-16)//2}+{
            
            (scr_height-height-32)//2}'
    	window.geometry(size_position)
    

PS:

  1. window also has two parameters winfo_width() and winfo_height() to get the width and height of the window, but note that this value is when the Windows10 screen is scaled to 100%.
  2. In layman's terms, tkinter thinks it is there, but after Tk generates the window, Windows will redraw the window according to the scaling algorithm, so it is not there in pixels;
  3. So if you want to get the real pixel width and height of the window, you have to multiply it by the zoom factor.

After knowing what I did, the boss looked at me differently!
awesome lad

If you've seen this far, then you must have dealt with tkinter. Believe me, if you don’t bookmark it now, when you face the same (screen width-window width)/2 in the future, you will think of a blogger who seems to have written different articles in detail, and you will never find mine (¬‿¬ )

©2021 CherryChenNan
likes and supports, collects and learns, pay attention to not get lost~

Guess you like

Origin blog.csdn.net/qq_27677599/article/details/117888750