1.Label&Button_标签和按钮(python可视化)

Label&Button_标签和按钮

1.代码

# -*- coding: utf-8 -*-
# @Time    : 2022/1/30 1:31
# @Author  : 斯嘉
# @File    : 1.Label&Button_标签和按钮.py
# @Emali   :[email protected]
# @Software: PyCharm
import tkinter as tk

window = tk.Tk()  # 创建一个窗口
window.title("My Window")  # 设置窗口的标题
window.geometry("200x150")  # 设置窗口的大小

var = tk.StringVar()
l = tk.Label(window, textvariable=var, bg="red", font=("Arial", 12), width=15, height=2)
l.pack()  # 放到window上

on_hit = False  # 定义一个布尔类型的值


# 按钮点击事件
def hit_me():
    global on_hit  # 全局变量
    if not on_hit:
        on_hit = True
        var.set("you hit me")
    else:
        on_hit = False
        var.set("")


b = tk.Button(window, text="hit me", width=15, height=2, command=hit_me)
b.pack()  # 放到window上

window.mainloop()

2.运行效果

https://img30.360buyimg.com/pop/jfs/t1/100837/35/22464/3253/61f5898aE2e31b8be/b461440542c11552.png

3.点击效果

  • 点击一下

  • 再点一下

https://img30.360buyimg.com/pop/jfs/t1/100837/35/22464/3253/61f5898aE2e31b8be/b461440542c11552.png

无标签
评论区
头像
文章目录