Simple Python happy journey: Gui topic of Python basic syntax

Defonds 2020-11-13 04:49:24
simple python happy journey gui


1. Python tkinter Set a specific or fixed window size

In the use of Python tkinter Set a specific size for the window , Use Tk() Class variable geometry() function .

from tkinter import *
gui = Tk()
gui.geometry("widthxheight")

among ,width and height It should be replaced by integer numbers representing the width and height of the window, respectively . Pay attention to the geometry() Of width and height There is one between the variables x.
Be careful : Notice the size of the window width x height The title window does not contain the title window .

1.1. Example 1 : stay Python tkinter Set the window size in

In this example , We will use geometry() The way to give Tk() Window set a fixed 500 ride 200 Size .

from tkinter import *
gui = Tk(className='Python Examples - Window Size')
# set window size
gui.geometry("500x200")
gui.mainloop()

Execution and output :
 stay Python tkinter Set the window size in .png
You can see , When the change procedure is being implemented , Will open a geometry() Function to define the size of the window GUI window .

1.2. Set window size

Now let's change the offer to geometry() The width and length of the function , such as 300 ride 300.

from tkinter import *
gui = Tk(className='Python Examples - Window Size')
# set window size
gui.geometry("300x300")
gui.mainloop()

Execution and output :
 Set window size .png

1.3. Summary

In this section, we will learn how to use the tkinter Of GUI Program setting window size .

2. Python tkinter Button example

2.1. Python tkinter Button

We will use Python Of tkinter Library to implement Python GUI Button in .
Add a button to tkinter The syntax of the window is as follows :

mybutton = Button(master, option=value)
mybutton.pack()

among master Is for the window you want to add to .tkinter The library provides different options for button constructors to change their appearance .

Options value
text Text display of button label
width Set the width of the button
height Set the height of the button
bg Sets the background color of the button
fg Set the font color of the button label
activebackground Set the background color after the button is clicked
activeforeground Set the font color of the button label after the button is clicked
command The function to call when the button is clicked
font Set the font size of the button label 、 Format
image Set button image

2.2. Example 1 : Use tkinter Library button

In the next example , We're going to create one tkinter Button .

from tkinter import *
gui = Tk(className='Python Examples - Button')
gui.geometry("500x200")
# create button
button = Button(gui, text='My Button', width=40, height=3, bg='#0052cc', fg='#ffffff', activebackground='#0052cc', activeforeground='#aaffaa')
# add button to gui window
button.pack()
gui.mainloop()

Execution and output :
 Use tkinter Library button .png

2.3. Summary

In this section, we learned how to use tkinter Library to create a GUI Button .

3. Click the button to call the function

When one tkinter When the button is pressed , You can use command Property to call a function . Assign the function name to which you want the button to be called when it is clicked command Attribute is enough .
Here is the pseudo code for calling a function when the button is clicked :

def someFunction:
function body
tkWindow = Tk()
button = Button(tkWindow, command=someFunction)

Or you can assign it after defining the button command:

def someFunction:
function body
tkWindow = Tk()
button = Button(tkWindow)
button['command'] = someFunction

3.1. Click the button to call the event function

from tkinter import *
from tkinter import messagebox
tkWindow = Tk()
tkWindow.geometry('400x150')
tkWindow.title('Defonds.net Tkinter Example')
def showMsg():
messagebox.showinfo('Message', 'You clicked the Submit button!')
button = Button(tkWindow, text='Submit', command=showMsg)
button.pack()
tkWindow.mainloop()

Execution and output :
 Click the button to call the event function .png
We need to pay attention when we call the function after we provide the user click button. :

  • Define the function before the button is set
  • command The value of the option is the function name without any quotation marks

4. Change font family 、 Font size and format

To change the font properties , Such as font series 、 font size 、 Font thickness, etc , have access to tkinter.font package .
In the program , introduce tkinter.font as font And provide variables to font.Font().

4.1. change tkinter The font series of buttons

In the following example , We will use family Name the parameter to font.Font() To change tkinter The font series of buttons .

from tkinter import *
import tkinter.font as font
gui = Tk(className='Python examples - Button')
gui.geometry('500x200')
# define font
myFont = font.Font(family='Helvetica')
# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font'] = myFont
# add button to gui window
button.pack()
gui.mainloop()

Execution and output :
 change tkinter The font series of buttons .png
If not provided font, This button will look like this :
 If not provided font, This button will look like this .png

4.2. change tkinter The font size of the button

We can also pass named parameters text to font.Font() To change tkinter The font size of the text in the button .

from tkinter import *
import tkinter.font as font
gui = Tk(className='Python examples - Button')
gui.geometry('500x200')
# define font
myFont = font.Font(size=30)
# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font'] = myFont
# add button to gui window
button.pack()
gui.mainloop()

Execution and output :
 change tkinter The font size of the button .png

4.3. change tkinter The font thickness of the button

We can also pass named parameters weight to font.Font() To change tkinter The font thickness of the text in the button .

from tkinter import *
import tkinter.font as font
gui = Tk(className='Python examples - Button')
gui.geometry('500x200')
# define font
myFont = font.Font(weight='bold')
# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font']=myFont
# add button to gui window
button.pack()
gui.mainloop()

Execution and output :
 change tkinter The font thickness of the button .png

4.4. change tkinter The font series of buttons 、 Font size and font thickness .

We can package and pass the above settings to font.Font().

from tkinter import *
import tkinter.font as font
gui = Tk(className='Python examples = Button')
gui.geometry('500x200')
# define font
myFont = font.Font(family='Helvetica', size=20, weight='bold')
# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font']=myFont
# add button to gui window
button.pack()
gui.mainloop()

Execution and output :
 change tkinter The font series of buttons 、 Font size and font thickness .png
We changed the font family to Courier Then run the program again :

myFont = font.Font(family='Courier', size=20, weight='bold')

Execution and output :
 We changed the font family to Courier Then run the program again .png

4.5. Summary

In this section , We use detailed examples , Demonstrates how to change tkinter The font series of buttons 、 Font size and font thickness .
Reference material

版权声明
本文为[Defonds]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database