Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Python GUI design the interface?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to design the interface of Python GUI". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The text and pictures of this article come from the network, only for learning, communication and use, do not have any commercial use, if you have any questions, please contact us in time to deal with.

1. Introduction of function method

The following function methods are based on the import of import tkinter and from tkinter import ttk. If you are using imort tkinter as tk, just replace the following tkinter with your defined alias.

Some of the function methods are simply introduced, and the details will be mentioned in later steps.

The method of placing and deleting components is universal.

2. Import tkinter library

Usually, if you import, just use the first line of code below to import.

Import tkinter

However, for some functions, you need to import a ttk module

Import tkinterfrom tkinter import ttk III. Windows

The delete window in step [5] is common to other components and will not be repeated later.

[1]。 Create

The way to create a window is relatively simple, just execute tkinter.Tk ().

Because we need to operate on the window, we have to use an instance object to save the window.

Import tkinterwindow = tkinter.Tk ()

[2]。 Set title

After the previous step is completed, the window is given a title named tk by default.

If we need to set a custom title, we need to use the title method.

Import tkinterwindow = tkinter.Tk () window.title ('title')

[3]。 Set Siz

When setting the size, using the geometry method, the x in the parameter is the English letter, and the case does not matter.

For the size of the 200x300, the first parameter 200 is width and the second parameter 300 is height.

If the window size is not set, the default size is 200x200

Import tkinterwindow = tkinter.Tk () window.title ('title') window.geometry ('200x300') [4]. Set background color

Use the configure method to set the value of the background parameter to change the background color.

The parameters passed in when changing the background color can be uppercase or lowercase

For other components, such as text, buttons, etc., you can also set the background color for the component.

You just need to change the instance object name window to the instance object name of the component.

Import tkinterwindow = tkinter.Tk () window.title ('title') window.geometry ('200x300') window.configure (background='BlUe')

[5]。 Delete window

Use

Import tkinterwindow = tkinter.Tk () window.title ('title') window.geometry ('200x300') window.configure (background='BlUe') window.destroy () 4, button

The method of placing buttons in steps [2] and [3] is common to other components.

[1]。 Create

Unlike windows, other components are not created directly into the window.

Button = tkinter.Button (window,text=' exit', command=lambda:window.destroy (), width=3,height=2)

[2]。 Place button (absolute position)

Button.place (relx=1,rely=1,anchor='se') # relative position, place button

[4]。 Code

If you want to place the button in the lower right corner of the window, you can set the anchor point to se, and then set the coordinates of the anchor point to the lower right corner of the window.

The horizontal and vertical axis coordinates must be given, and the anchor point parameter anchor can be omitted, but for special application scenarios, we have to use the anchor parameter to set the anchor point.

Import tkinterwindow = tkinter.Tk () window.title ('title') window.geometry ('200x300') button = tkinter.Button (window,text=' exit', command=lambda:window.destroy (), width=5,height=2) # # button.place (Xerox 40 ~ (10) # absolute position, place button button.place (relx=1,rely=1,anchor='se') # relative position, place button five, single-line text

[1]。 Create

If the incoming color or text content needs to be input, you can replace the content after the equal sign with the corresponding string variable

Text= tkinter.Label (window,bd=4,fg='red',bg='white',text=' content 111111111111111')

[2]。 Code

Import tkinterwindow = tkinter.Tk () window.title ('title') window.geometry ('200x200') text= tkinter.Label (window,bd=4,fg='red',bg='white',text=' content 111111111111111') # # text.place (Xero20 relx=0.2,rely=0.2 50) # absolute position, put the text text.place (relx=0.2,rely=0.2) # relative position, put the text "how to design the interface" content here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report