官术网_书友最值得收藏!

Preventing the GUI from being resized

Getting ready

This recipe extends the previous one. Therefore, it is necessary to have typed Recipe 1 yourself into a project of your own or downloaded the code from https://www.packtpub.com/support.

How to do it...

We are preventing the GUI from being resized.

import tkinter as tk        # 1 imports

win = tk.Tk()               # 2 Create instance
win.title("Python GUI")     # 3 Add a title       
    
win.resizable(0, 0)         # 4 Disable resizing the GUI

win.mainloop()              # 5 Start GUI

Running the code creates this GUI:

How it works...

Line 4 prevents the Python GUI from being resized.

Running this code will result in a GUI similar to the one we created in Recipe 1. However, the user can no longer resize it. Also, notice how the maximize button in the toolbar of the window is grayed out.

Why is this important? Because, once we add widgets to our form, resizing can make our GUI look not as good as we want it to be. We will add widgets to our GUI in the next recipes.

Resizable() is a method of the Tk() class and, by passing in (0, 0), we prevent the GUI from being resized. If we pass in other values, we hard-code the x and y start up size of the GUI, but that won't make it nonresizable.

We also added comments to our code in preparation for the recipes contained in this book.

Note

In visual programming IDEs such as Visual Studio .NET, C# programmers often do not think of preventing the user from resizing the GUI they developed in this language. That creates inferior GUIs. Adding this one line of Python code can make our users appreciate our GUI.

主站蜘蛛池模板: 新兴县| 阳东县| 裕民县| 巨鹿县| 剑河县| 恩平市| 图片| 竹山县| 阿瓦提县| 镇平县| 宕昌县| 嘉兴市| 河东区| 惠东县| 永年县| 长岛县| 南郑县| 那曲县| 左权县| 杭锦后旗| 杨浦区| 句容市| 瓦房店市| 巢湖市| 垦利县| 莲花县| 金湖县| 五台县| 辰溪县| 山西省| 满城县| 贵溪市| 南开区| 翁源县| 焉耆| 新绛县| 报价| 西和县| 奉新县| 通化市| 蓝山县|