importtkinterastkdefchange_color():# 修改按钮的背景色和前景色button.config(bg="green",fg="black")# 创建主窗口root=tk.Tk()root.title("修改 Button 颜色示例")# 创建按钮并设置初始颜色button=tk.Button(root,text="点击我",bg="blue",fg="white",command=change_color)button.pack(pady=20)# 运...
导入库:我们首先导入tkinter和random,后者用于生成随机颜色。 创建主窗口:通过tk.Tk()创建主窗口,并设置窗口标题。 创建标签:使用tk.Label来显示当前按钮的背景颜色,并设置字体和边距。 创建按钮:使用tk.Button创建一个按钮,设置按钮文本和字体,并关联command参数到change_color函数。 改变颜色的函数:change_color函数产...
在Tkinter中,你可以使用config方法来更改按钮的颜色。以下是一个简单的示例,展示了如何在单击一个按钮后更改另一个按钮的颜色: 代码语言:txt 复制 import tkinter as tk def change_color(): button2.config(bg="blue") root = tk.Tk() button1 = tk.Button(root, text="点击我", command=ch...
1、为Button组件(按钮)绑定回调函数 import tkinter as tk def say_hello(): print("Hello World!") root = tk.Tk() button = tk.Button(root, text="点我", command=say_hello) button.pack() root.mainloop() 2、为Checkbutton组件(多选择钮)绑定回调函数 import tkinter as tk def show_selection(...
button_border_size = Label(root, text='按钮边框大小:') self.border = Button(root, text='按钮边框', bd=5) # 按钮颜色,bg='背景色', fg='前景色' self.the_button_color = Label(root, text='按钮颜色:') self.button_background_colour = Button(root, text='背景色', bg='blue') self....
color = askcolor(title="颜色选择框", color="red") root.config(bg=color[1])# askcolor 返回的是元组类型;例如((255.99609375, 0.0, 0.0), '#ff0000')tk.Button(root, text="背景颜色", command=change_color).pack() root.mainloop() 效果演示...
import tkinter as tk from tkinter import ttk style = ttk.Style() style.theme_use('alt') style.configure('TButton', background = 'red', foreground = 'white', width = 20, borderwidth=1, focusthickness=3, focuscolor='none') style.map('TButton', background=[('active','red')]) roo...
root=Tk() root.title('1号窗口') # 显示内置图片 # x = Label(root, bitmap='warning') l=Label(root, fg='red', bg='blue',text='wangwei', width=34, height=10) l.pack() # command 指定按钮调用的函数 b=Button(root, text='clickme', command=btn_click) ...
root = Tk() def callback(): fileName = colorchooser.askcolor() print(fileName) Button(root, text="选择颜色", command=callback).pack() mainloop() 参数 askcolor(color, ** option)函数的color参数用于指定初始化的颜色,默认是浅灰色;option参数可以指定的选项及含义如下表所示 ...
Tkinter 按钮组件用于在 Python 应用程序中添加按钮,按钮上可以放上文本或图像,按钮可用于监听用户行为,能够与一个 Python 函数关联,当按钮被按下时,自动调用该函数。语法语法格式如下:w = Button ( master, option=value, ... )master: 按钮的父容器。 options: 可选项,即该按钮的可设置的属性。这些选项可以...