Python-written Tkinter program screen centering method

Source: Internet
Author: User
This article applies to the scenario: want to use Tkinter development interface Program and Screen center, but did not find the corresponding API.

These two days play tkinter, feel good, is the screen center this problem searched the internet for a long time also did not
Find the answer, finally no way, look at its documents, in its own way to achieve.

The method is very earthy, that is, to get the initialized form size and screen size, and then calculate the approximate value.
Here's the code:
Copy the Code code 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+%d '% (Curwidth,curheight,
(scnwidth-curwidth)/2, (Scnheight-curheight)/2)
Rt.geometry (TMPCNF)
Rt.mainloop ()

All right, that's it, I hope it helps 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.