程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

On Tkinter of Python visual programming (I)

編輯:Python

One 、 know tkinter

tkinter yes Python Standards for GUI library , Tkinter modular (Tk Interface ) yes Python Standards for Tk GUI Interface to toolkit .Tk and Tkinter Can be in most of Unix Use under platform , It can also be applied to Windows and Macintosh In the system .Tk8.0 Later versions of can implement local window style , And works well on most platforms . So when you use it, just import That's all right. .

The code is as follows

import tkinter

Two 、 Create a simple interface

import tkinter
# Create a window object
screen = tkinter.Tk()
# Use mainloop Method to make the window display
screen.mainloop()

The effect of the above code after execution is shown in the following figure

3、 ... and 、 Start layout interface

The interface is so dry that it doesn't look good , So we need to make a basic layout of our interface , before this , We need to learn some controls , And how to make them show

1、label Control

For the front-end partners, this label is very familiar , This is an ordinary text display , Let's see how it is realized here

The code is as follows

import tkinter
# Create a window object
screen = tkinter.Tk()
# establish label object
label = tkinter.Label(screen,text=" This is a label Control ")
# Show label,pack The function is adaptive
label.pack()
# Use mainloop Method to make the window display
screen.mainloop()

The effect is shown in the figure

2、entry Control

This is an input box control , Like the front end text type , How is this achieved , Don't talk much , Code up

import tkinter
# Create a window object
screen = tkinter.Tk()
# establish label object
label = tkinter.Label(screen,text=" This is a label Control ")
# Show label,pack The function is adaptive
label.pack()
# The following three lines of code are generally connected , The first line is to set and get the input data
u = tkinter.StringVar()
text = tkinter.Entry(screen, textvariable=u)
text.pack()
# Use mainloop Method to make the window display
screen.mainloop()

The effect is shown below

Let's set the value in the input box first , Then get

import tkinter
# Create a window object
screen = tkinter.Tk()
# establish label object
label = tkinter.Label(screen,text=" This is a label Control ")
# Show label,pack The function is adaptive
label.pack()
# The following three lines of code are generally connected , The first line is to set and get the input data
u = tkinter.StringVar()
text = tkinter.Entry(screen, textvariable=u)
text.pack()
u.set("hello")
print(u.get())
# Use mainloop Method to make the window display
screen.mainloop()

The effect is shown in the figure

3、button Control

This control is the button , Some functions can be realized by clicking , What about this button How to add it , And how to realize the functions inside , Come on code

import tkinter
# Create a window object
screen = tkinter.Tk()
# establish label object
label = tkinter.Label(screen,text=" This is a label Control ")
# Show label,pack The function is adaptive
label.pack()
# The following three lines of code are generally connected , The first line is to set and get the input data
u = tkinter.StringVar()
text = tkinter.Entry(screen, textvariable=u)
text.pack()
u.set("hello")
def run():
print(u.get())
# among commend The value of is the function name
button = tkinter.Button(screen, text=" Am I ", command=run)
button.pack()
# Use mainloop Method to make the window display
screen.mainloop()

The effect is as shown in the picture

Four 、 summary

That's all tkinter Explain some basic parts of , I believe you also have a certain understanding , Then the next article will update the intermediate part !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved