3683 views|0 replies

7249

Posts

192

Resources
The OP

Python Tinker Study Notes (Part 2) [Copy link]

This post was last edited by Changjianze1 on 2019-2-20 13:23 This content was originally created by EEWORLD forum netizen Changjianze1. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source Let's review first. This is the most basic. Every program needs Tkinter. The program only needs to do three things: The smallest [Tkinter] program (based on Python3, Python2 is slightly different) from tkinter import * # import the Tkinter module root = Tk() # create a root window root.mainloop() # create an event loop Four input boxesEntry 0000] English: The input box Entry is one of the most commonly seen interface controls. For example, it is needed to enter the login account password, such as the serial port assistant to enter the data to be sent, etc., so let's take a look at the syntax of this control. The syntax format is as follows: w = Entry( master, option, ... ) option is generally used: bg, font, show, etc. Example:
  1. from tkinter import * # import the Tkinter module root = Tk() # create a root window L1 = Label(root,text="Please enter") L1.pack(side = LEFT) E1 = Entry(root,bd =5) E1.pack(side = RIGHT) root.mainloop()
复制代码
Running effect: 5. Text box Text The text box Text is also a very commonly used control. For example, in the serial port assistant that everyone often uses, Text is often used to display the data received by the serial port. When the serial port receives data, text displays the received data on the interface, so let's learn it! ! The Text control can display web links, pictures, HTML pages, and even CSS style sheets. Application method: 1. Method to set the text of the Python Tkinter Text control text.insert(index,string) index = xy format, x represents the row, y represents the column Insert data into the first row, text.insert(1.0,'hello world') 2. Method to clear the text of the Python Tkinter Text control # Idea: clear from the first row to the last row text.delete(1.0,Tkinter.END) Example:
  1. from tkinter import * # import the Tkinter module root = Tk() # create a root window text = Text(root, width=50, height=10) #30 means the width of 30 average characters, and the height is set to two lines text.pack() text.insert(INSERT, '0x55 ') #INSERT indicates the position of the input cursor. The input cursor after initialization is in the upper left corner by default text.insert(INSERT, '0x66 ') text.insert(INSERT, '0x77 ') text.insert(INSERT, '0x88 ') text.insert(INSERT, '0x99 ') text.insert(INSERT, '0xAA ') text.insert(INSERT, '0xBB ') text.insert(INSERT, '0xCC ') text.insert(INSERT, '0xDD ') text.insert(END, 'show is over') root.mainloop()
复制代码
Running effect: 6. Layout control Several of the most important controls have been basically learned. I mainly want to imitate and make a simple interface. Now let's start looking at some page control layouts. Tkinter has three geometric layout managers, namely: pack layout, grid layout, and place layout. Pack layout has been used in the previous sections, and complex applications are not used for the time being. Mainly look at the grid layout. The grid layout is also called the grid layout, which is the most recommended layout. Most programs have rectangular interfaces. We can easily divide it into a grid of several rows and columns, and then place components in the grid according to the row and column numbers. When using the grid layout, you need to specify two parameters in it, using row to represent the row and column to represent the column. It should be noted that the serial numbers of row and column start from 0. from tkinter import * # import the Tkinter module root = Tk() # create a root window root.title('title') root.geometry('300x200') label1 = Label(root, text = 'i am label1') label1.grid(row=0) label2 = Label(root, text = 'i am label2') label2.grid(row=0, column=1) root.mainloop()

微信图片_20190220083935.png (113.21 KB, downloads: 0)

微信图片_20190220083935.png

微信图片_20190220084011.png (21.38 KB, downloads: 0)

微信图片_20190220084011.png

微信图片_20190220084046.png (14.4 KB, downloads: 0)

微信图片_20190220084046.png

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    Robot
    development
    community

    About Us Customer Service Contact Information Datasheet Sitemap LatestNews

    Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

    Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
    快速回复 返回顶部 Return list