python based tutorial: Python: tkinter window centered on screen, set the maximum window, the minimum size examples

This article describes the Python: tkinter window centered on screen, set the maximum window size of the smallest instance, has a good reference value, we want to help. Xiao Bian together to follow up to see it
I ado. Look at the code directly to it!

#!/usr/bin/env python
#coding=utf-8
'''
  窗口屏幕居中,设置窗口最大,最小尺寸...
  版权所有 2014 yao_yu (http://blog.csdn.net/yao_yu_126)
  本代码以MIT许可协议发布
  2014-04-15 创建
'''
  
import tkinter as tk
from tkinter  import ttk
  
def get_screen_size(window):
  return window.winfo_screenwidth(),window.winfo_screenheight()
  
def get_window_size(window):
  return window.winfo_reqwidth(),window.winfo_reqheight()
  
def center_window(root, width, height):
  screenwidth = root.winfo_screenwidth()
  screenheight = root.winfo_screenheight()
  size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
  print(size)
  root.geometry(size)
  
root = tk.Tk()
root.title('测试窗口')
center_window(root, 300, 240)
root.maxsize(600, 400)
root.minsize(300, 240)
ttk.Label(root, relief = tk.FLAT, text = '屏幕大小(%sx%s)\n窗口大小(%sx%s)' % (get_screen_size(root) + get_window_size(root))).pack(expand = tk.YES)
tk.mainloop()

Supplementary information: About window tkinter need to use several settings

Set the size and position of the window

geometry(widthxheight+x+y)

The length and width of the window width and height, respectively;

Window to the main window of the pitch x and Y;

Note the minus sign may be used, for example, 10 * 10 10x10-10-10 Representative lower right corner of the window size, but can not directly cause x or y is negative, then into wxh + x + y; can not have simultaneously the format spaces.

When no parameters, this method can return the current size of the position parameter.

# -*- coding:utf-8 -*-
from tkinter import *
 
root = Tk()
width, height, padx, pady = 800, 600, 40, 300
root.geometry('%dx%d-%d+%d' % (width, height, padx, pady)) 

Set window style, transparent and full-screen

-toolwindow window can be set for the toolbar style;

-alpha transparency can be provided, completely transparent 0, 1 opaque. Here are all the contents within the transparent window, not just the form, so be especially careful a fully transparent window!

Note that full screen disposed in front of -fullscreen dash (-) is not less

-topmost settings window on top. At the same time the top two windows to the same level (can cover each other), but at the same time they can not be set to cover the top of the window.

root.attributes('-toolwindow', False, 
        '-alpha', 0.9, 
        '-fullscreen', True, 
        '-topmost', True)

Questions: a full-screen, top, transparent, nothing out of the window title bar will mean?

I write to you, for everyone to recommend a very wide python learning resource gathering, click to enter , there is a senior programmer before learning to share

Experience, study notes, there is a chance of business experience, and for everyone to carefully organize a python to combat zero-based item of information,

Day to you on the latest technology python, prospects, learning to leave a message of small details
than this Python: tkinter window centered on screen, set the maximum window size of the smallest example is the small series to share the entire contents of all of the

Published 47 original articles · won praise 34 · views 60000 +

Guess you like

Origin blog.csdn.net/haoxun08/article/details/104887373