ITPub博客

首页 > IT职业 > IT图书 > python如何创建操作页面

python如何创建操作页面

IT图书 作者:LeeC 时间:2021-09-11 09:56:27 0 删除 编辑

说明

Python自带tkinter模块,本质上是GUI工具包TK的Python编程界面,提供了快速方便地创建GUI应用的方法。

过程

(1)导入tkinter相关的模块。

(2)通过pack()方法定义初始化函数,将组件传递到父容器。

(3)定制创建组件的方法,我们创建一个标签和一个按钮,这个按钮被点击之后,就会触发who方法。

(4)通过messagebox显示提示框。

(5)对APP进行实例化,然后通过主线程监听界面操作。

实例

from tkinter import *
import tkinter.messagebox as messagebox
 
class MyApp(Frame):
 
    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.pack()
        self.createWidgets()
 
    def createWidgets(self):
        self.helloLabel = Label(self,text="世界上最帅的人是谁?")
        self.helloLabel.pack()
        self.quitButton = Button(self,text="谁呢?",command=self.who)
        self.quitButton.pack()
 
    def who(self):
        messagebox.showinfo("答案","当然是小帅b啦")
 
myapp = MyApp()
myapp.master.title('hello')
myapp.mainloop()

以上就是python创建操作页面的方法,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4352/viewspace-2830125/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    86
  • 访问量
    659