这一章学习添加lable(样式)和button(按钮)
添加按钮
from tkinter import *
def new_button(self):
"""
设置按钮的函数
:return:
"""
# anchor="n" 代表text的值在该组件的北边 还有 s w e sw se nw ne
self.button_01 = tk.Button(root,text="登录",width=6,height=1,anchor="n",command=self.login)
self.button_01.pack()
global photo
photo = tk.PhotoImage(file="./imgs/2.gif",width=100,height=100)
# state="disabled" 表示禁用该按钮
self.button_02 = tk.Button(root,image=photo,command=self.login,state="disabled")
self.button_02.pack()
tk.Button(root) 这个函数就是添加按钮
self.button_01.pack() pack()是布局管理器,目前可以理解为 表示显示在窗口上
photo = tk.PhotoImage(file="./imgs/2.gif",width=100,height=100) 表示封装指定图片后续会应用
实参:
width、height设置按钮的宽和高 单位为像素 (1个英文占1个像素,1个中文占2个像素)
state="disabled" 表示禁用该按钮
添加样式
from tkinter import *
def new_lable(self):
"""
lable(样式) 用法
:return:
"""
self.lable_01 = tk.Label(self,text="你是帅哥吗",width=10,height=2,bg="black",fg="white")
# 以键值对的形式重构lable_01的text值
self.lable_01["text"] = "你是谁"
# 以config方法重构lable_01的前景颜色和背景颜色
self.lable_01.config(fg="red",bg="green")
self.lable_01.pack()
self.lable_02 = tk.Label(self, text="你是美女吗", width=10, height=2, bg="blue", fg="white",font=("黑体",15))
self.lable_02.pack()
# 显示图像
global photo
photo = tk.PhotoImage(file=f"./imgs/2.gif")
self.lable_03 = tk.Label(self,image=photo)
self.lable_03.pack()
# 多行文字输出
# borderwidth=1: 这是标签的边框宽度,设置为1个像素
# relief="solid": 这是标签的边框样式,设置为实线边框 raised(凸起) sunken(凹陷) flat(扁平) groove(透明) ridge(剪影)
# justify="right": 这是标签文本的对齐方式,设置为右对齐 left(左对齐) center(居中对齐)
self.lable_04 = tk.Label(self,text="whoami\ni am who\n666",borderwidth=1,relief="solid",justify="center")
self.lable_04.pack()
基础在主窗口上面添加样式代码如上所示
就不详细解释了 代码里全是注释
结合第1、2章的基础代码 如下所示
import tkinter as tk
from tkinter import messagebox
class Appclication(tk.Frame):
"""
继承tk.Frame
"""
def __init__(self,master=None):
# supper()代表父类
super().__init__(master=master)
self.master = master
self.pack()
self.new_button()
self.new_lable()
def new_button(self):
"""
设置按钮的函数
:return:
"""
# anchor="n" 代表text的值在该组件的北边 还有 s w e sw se nw ne
self.button_01 = tk.Button(root,text="登录",width=6,height=1,anchor="n",command=self.login)
self.button_01.pack()
global photo
photo = tk.PhotoImage(file="./imgs/2.gif",width=100,height=100)
# state="disabled" 表示禁用该按钮
self.button_02 = tk.Button(root,image=photo,command=self.login,state="disabled")
self.button_02.pack()
def new_lable(self):
"""
lable(样式) 用法
:return:
"""
self.lable_01 = tk.Label(self,text="你是帅哥吗",width=10,height=2,bg="black",fg="white")
# 以键值对的形式重构lable_01的text值
self.lable_01["text"] = "你是谁"
# 以config方法重构lable_01的前景颜色和背景颜色
self.lable_01.config(fg="red",bg="green")
self.lable_01.pack()
self.lable_02 = tk.Label(self, text="你是美女吗", width=10, height=2, bg="blue", fg="white",font=("黑体",15))
self.lable_02.pack()
# 显示图像
global photo
photo = tk.PhotoImage(file=f"./imgs/2.gif")
self.lable_03 = tk.Label(self,image=photo)
self.lable_03.pack()
# 多行文字输出
# borderwidth=1: 这是标签的边框宽度,设置为1个像素
# relief="solid": 这是标签的边框样式,设置为实线边框 raised(凸起) sunken(凹陷) flat(扁平) groove(透明) ridge(剪影)
# justify="right": 这是标签文本的对齐方式,设置为右对齐 left(左对齐) center(居中对齐)
self.lable_04 = tk.Label(self,text="whoami\ni am who\n666",borderwidth=1,relief="solid",justify="center")
self.lable_04.pack()
def shuaige(self):
"""
定义被绑定函数
:return:
"""
messagebox.showinfo("帅哥","你肯定是帅哥")
def login(self):
"""
定义登录函数
:return:
"""
messagebox.showinfo(title="学习系统",message="欢迎大佬登录系统学习!")
if __name__ == '__main__':
root = tk.Tk()
root.geometry("300x300-200+200")
root.title("GUI编程学习_03")
app = Appclication(master=root)
root.mainloop()
运行后如下所示
片尾贴上我学习gui编程的视频,推荐结合观看
【Python-GUI图形界面编程_GUI编程实例_快速入门】 https://www.bilibili.com/video/BV1EP411P7wi/?p=17&share_source=copy_web&vd_source=bfaefb38ccfecdbc891cd2b434d819d4