The screen Center method of the Tkinter program written in Python

Source: Internet
Author: User
This article mainly introduces the screen Center method of the Tkinter program written in Python. Tkinter is a python module and an interface that calls TclTk. it is a cross-platform GUI interface for scripts, if you want to use Tkinter to develop the interface program and center the screen, you can refer to the following application scenarios: you cannot find the corresponding API.

I have been playing Tkinter these two days. I feel good, that is, the problem of center of the screen has not been found online for a long time.
Find the answer and there is no way at last. read its documentation and implement it in your own way.

The method is very earthy, that is, get the initial form size and screen size, and then obtain the general value through calculation.
The following code is used:

The code is as follows:


#! /Usr/bin/python
'''
File: screenCenter. pyw
Author: Mike
E-mail: Mike_Zhang@live.com
'''
From Tkinter import *

Rt = Tk ()
Rt. resizable (False, False)
Rt. title ("Screen center ")

Rt. update () # update window, must do
CurWidth = rt. winfo_reqwidth () # get current width
CurHeight = rt. winfo_height () # get current height
ScnWidth, scnHeight = rt. maxsize () # get screen width and height
# Now generate configuration information
Tmpcnf = '% dx % d + % d' % (curWidth, curHeight,
(ScnWidth-curWidth)/2, (scnHeight-curHeight)/2)
Rt. geometry (tmpcnf)
Rt. mainloop ()

Okay, that's all. I hope it will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.