• Python Gui之tkinter


    GUI是什么

    目录

    1。GUI编程的核心步骤和第一个GUI程序

    2.tkinter主窗口​​​​​​​

    3.GUI的整体描述

    常用组件汇总

    4.简单的组件

    1.Label标签

    2.Options选项详解

    3.Button

    4.Entry单行文本框

    5.Text多行文本框



    1。GUI编程的核心步骤和第一个GUI程序

    1. from tkinter import *
    2. from tkinter import messagebox
    3. root = Tk()
    4. btn01 = Button(root)
    5. btn01['text'] = '点我就送花'
    6. btn01.pack()
    7. def songhua(e): #e就是事件对象
    8. messagebox.showinfo("Message","送你一朵玫瑰花")
    9. print("送你99朵玫瑰花")
    10. btn01.bind("",songhua)
    11. root.mainloop() #调用组件的mainloop()方法,进入事件循环

    2.tkinter主窗口

    1. root.title("我的第一个GUI程序") #显示程序名称
    2. root.geometry("500x300+100+200") #生成一个长500宽300离左边100上面200的窗口

    3.GUI的整体描述

    常用组件汇总

    Widget

    1. '''经典的Gui类的写法,使用面向对象的方法'''
    2. from tkinter import *
    3. from tkinter import messagebox
    4. class Application(Frame):
    5. '''一个经典的Gui的类的写法'''
    6. def __init__(self,master=None):
    7. super().__init__(master) #super()代表的是父类的定义,而不是父类对象
    8. self.master = master
    9. self.pack()
    10. self.createWideget()
    11. def createWideget(self):
    12. '''创建组件'''
    13. self.btn01 = Button(self)
    14. self.btn01["text"] = "点击送花"
    15. self.btn01.pack()
    16. self.btn01["command"] = self.songhua
    17. # 创建一个退出按钮
    18. self.btnQuit = Button(self, text="退出", command=root.destroy)
    19. self.btnQuit.pack()
    20. def songhua(self):
    21. messagebox.showinfo("送花","送你99朵玫瑰花")
    22. if __name__ == '__main__':
    23. root = Tk()
    24. root.geometry("500x100+200+300")
    25. root.title("一个经典的GUI程序的类的测试")
    26. app = Application(master=root)
    27. root.mainloop()

    4.简单的组件

    1.Label标签

    1.width,height:用于指定区域大小,如果显示是文本,则以单个英文字符大小为单位(一个汉字宽度占2个字符位置,高度和英文字符一样);如果显示是图像,则以像素为单位。默认值是根据具体显示的内容动态调整。

    2.font指定字体和字体大小,如:font = (font_name,size)

    3.image:显示在Label上的图像,目前tkinter只支持gif格式。

    4.fg和bgfg(foreground):前景色、bg(background):背景色

    5.justify针对多行文字的对齐,可设置justify属性,可选值"left","center" or "right"

    1. '''经典的Gui类的写法,使用面向对象的方法'''
    2. from tkinter import *
    3. from tkinter import messagebox
    4. class Application(Frame):
    5. '''一个经典的Gui的类的写法'''
    6. def __init__(self,master=None):
    7. super().__init__(master) #super()代表的是父类的定义,而不是父类对象
    8. self.master = master
    9. self.pack()
    10. self.createWideget()
    11. def createWideget(self):
    12. '''创建组件'''
    13. self.label01 = Label(self, text="测试label", width=10, height=2, bg="black", fg="white")
    14. self.label01.pack()
    15. self.label02 = Label(self, text="测试label2", width=10, height=2, bg="blue", fg="white", font=("黑体",30))
    16. self.label02.pack()
    17. '''显示图像'''
    18. global photo #把photo声明全局变量。如果是局部变量,本方法执行完毕后,图像对象销毁,窗口显示不出图像
    19. photo = PhotoImage(file="img/2.gif")
    20. self.label03 = Label(self, image=photo)
    21. self.label03.pack()
    22. '''多行文本'''
    23. self.label04 = Label(self, text="DNSscan\nauther:Darling\n嘤嘤嘤", borderwidth=5, relief="groove", justify="right")
    24. self.label04.pack()
    25. if __name__ == '__main__':
    26. root = Tk()
    27. root.geometry("500x400+200+300")
    28. root.title("测试label组件")
    29. app = Application(master=root)
    30. root.mainloop()

    2.Options选项详解

    通过学习Label组件,我们发现可以通过Options设置组件的属性,从而控制组件的各种状态。比如:宽度、高度、颜色、位置等等。我们可以通过三种方式设置Options选项,这在各种GUI组件中用法都一致。

    1.创建对象时,使用可变参数fred=Button(self,fg="red",bg="blue")

    2.创建对象后,使用字典索引方式fred["fg"]="red"fred["bg"]="blue"

    3.创建对象后,使用config()方法fred.config(fg="red",bg="blue")

    3.Button

    Button(按钮)用来执行用户的单击操作。Button可以包含文本,也可以包含图像。按钮被单击后会自动调用对应事件绑定的方法。

    1. '''经典的Gui类的写法,使用面向对象的方法'''
    2. from tkinter import *
    3. from tkinter import messagebox
    4. class Application(Frame):
    5. '''一个经典的Gui的类的写法'''
    6. def __init__(self,master=None):
    7. super().__init__(master) #super()代表的是父类的定义,而不是父类对象
    8. self.master = master
    9. self.pack()
    10. self.createWideget()
    11. def createWideget(self):
    12. '''创建组件'''
    13. self.btn01=Button(root, text="登录", command=self.login)
    14. self.btn01.pack()
    15. global photo
    16. photo = PhotoImage(file="img/2.gif")
    17. self.btn02 = Button(root, image=photo, command=self.login)
    18. self.btn02.pack()
    19. def login(self):
    20. messagebox.showinfo("DNSscan一体化渗透框架","登录成功")
    21. if __name__ == '__main__':
    22. root = Tk()
    23. root.geometry("500x400+200+300")
    24. root.title("测试Button组件")
    25. app = Application(master=root)
    26. root.mainloop()

    4.Entry单行文本框

    Entry用来接收一行字符串的控件。如果用户输入的文字长度长于Entry控件的宽度时,文字会自动向后滚动。如果想输入多行文本,需要使用Text控件。

    1. '''经典的Gui类的写法,使用面向对象的方法'''
    2. from tkinter import *
    3. from tkinter import messagebox
    4. class Application(Frame):
    5. '''一个经典的Gui的类的写法'''
    6. def __init__(self,master=None):
    7. super().__init__(master) #super()代表的是父类的定义,而不是父类对象
    8. self.master = master
    9. self.pack()
    10. self.createWideget()
    11. def createWideget(self):
    12. '''创建登录组件'''
    13. self.label01 = Label(self, text="用户名")
    14. self.label01.pack()
    15. #StringVar变量绑定到指定的组件
    16. # StringVar变量的值发生变化,组件内容也发生变化;
    17. #组件内容发生变化,StringVar变量的值也发生变化
    18. v1 = StringVar()
    19. self.entry01 = Entry(self, textvariable=v1)
    20. self.entry01.pack()
    21. v1.set("admin")
    22. print(v1.get());print(self.entry01.get())
    23. '''创建密码框'''
    24. self.label02 = Label(self, text="密码")
    25. self.label02.pack()
    26. v2 = StringVar()
    27. self.entry02 = Entry(self, textvariable=v2, show="*")
    28. self.entry02.pack()
    29. # self.btn01 = Button(self, text="登录", command=self.login)
    30. # self.btn01.pack()
    31. Button(self, text="登录",command=self.login).pack()
    32. def login(self):
    33. username = self.entry01.get()
    34. pwd =self.entry02.get()
    35. print("去数据库对比用户名和密码!")
    36. print("用户名"+username)
    37. print("密码" + pwd)
    38. if username=="admin" and pwd=="admin" :
    39. messagebox.showinfo("DNSscan一体化渗透框架","登录成功,欢迎使用DNSscan一体化渗透框架")
    40. else:
    41. messagebox.showinfo("DNSscan一体化渗透框架","登录失败!用户名或密码错误")
    42. if __name__ == '__main__':
    43. root = Tk()
    44. root.geometry("500x400+200+300")
    45. root.title("测试Button组件")
    46. app = Application(master=root)
    47. root.mainloop()

    5.Text多行文本框

    Text(多行文本框)的主要用于显示多行文本,还可以显示网页链接,图片,HTML页面,甚至CSS样式表,添加组件等。因此,也常被当做简单的文本处理器、文本编辑器或者网页浏览器来使用。比如IDLE就是Text组件构成的。

    1. '''经典的Gui类的写法,使用面向对象的方法'''
    2. from tkinter import *
    3. from tkinter import messagebox
    4. import webbrowser
    5. class Application(Frame):
    6. '''一个经典的Gui的类的写法'''
    7. def __init__(self,master=None):
    8. super().__init__(master) #super()代表的是父类的定义,而不是父类对象
    9. self.master = master
    10. self.pack()
    11. self.createWideget()
    12. def createWideget(self):
    13. '''创建登录组件'''
    14. self.w1 = Text(root, width=40, height=12, bg="gray")
    15. #宽度20个字母(10个汉字),高度一个行高
    16. self.w1.pack()
    17. self.w1.insert(1.0, "0123456789\nabcdefg")
    18. self.w1.insert(2.3, "嘤嘤嘤嘤嘤嘤嘤嘤嘤\n")
    19. Button(self, text="重复插入文本", command=self.insertText).pack(side="left")
    20. Button(self, text="返回文本", command=self.returnText).pack(side="left")
    21. Button(self, text="添加图片", command=self.addImage).pack(side="left")
    22. Button(self, text="添加组件", command=self.addWidget).pack(side="left")
    23. Button(self, text="通过tag精确控制文本", command=self.testTag).pack(side="left")
    24. def insertText(self):
    25. # INSERT索引表示在光标处插入
    26. self.w1.insert(INSERT, 'GAOqi')
    27. #END索引表示在最后插入
    28. self.w1.insert(END, '[sxt]')
    29. def returnText(self):
    30. # Index(索引)是用来指向Text组件中的文本的位置,Text的组件索引也是对应实际字符之间的位置
    31. # 核心:行号以1开始,列号以0开始
    32. print(self.w1.get(1.2, 1.6))
    33. self.w1.insert(1.8, "gaoqi")
    34. print("所有文本内容:\n"+self.w1.get(1.0, END))
    35. def addImage(self):
    36. # global photo
    37. self.photo = PhotoImage(file="img/2.gif")
    38. self.w1.image_create(END,image=self.photo)
    39. def addWidget(self):
    40. b1 =Button(self.w1, text='DNSscan')
    41. #在text创建组件的命令
    42. self.w1.window_create(INSERT, window=b1)
    43. def testTag(self):
    44. self.w1.delete(1.0, END)
    45. self.w1.insert(INSERT, "dnsSCAN\ndnsscan------>\n[+]")
    46. self.w1.tag_add("good", 1.0, 1.9)
    47. self.w1.tag_config("good", background="yellow", foreground="red")
    48. self.w1.tag_add("baidu", 4.0, 4.2)
    49. self.w1.tag_config("baidu", underline=True)
    50. self.w1.tag_bind("baidu", "", self.webshow)
    51. def webshow(self, event):
    52. webbrowser.open("http://www.baidu.com")
    53. if __name__ == '__main__':
    54. root = Tk()
    55. root.geometry("500x400+200+300")
    56. root.title("测试Text多行文本组件")
    57. app = Application(master=root)
    58. root.mainloop()

  • 相关阅读:
    做室内装修设计需要什么资质,办理装修设计资质办理标准是怎样的
    SwitchyOmega_Chromium插件的下载安装以及使用
    aws入门实践-Cloud9的安装和使用
    贪心算法(又叫贪婪算法)Greedy Algorithm
    计算机组成原理习题课第三章-4(唐朔飞)
    老忘记带伞,自己动手做一个雨天提醒打伞小工具
    Java – 从资源文件夹中读取文件
    ATFX汇市:8月名义与核心CPI走势分化,美国通胀率算升高还是降低?
    Stable Diffusion WebUI中COMMANDLINE_ARGS参数配置说明
    DaoCloud贾恒:一文搞懂发布现代化
  • 原文地址:https://blog.csdn.net/qq_52074678/article/details/127995794