单击时颜色按钮背景永久,没有压力效果?
Color button background permanently when clicked, and without pressure effect?
我构建了一个按钮图像(第一张图像),但我手动将相邻文本添加为纯文本。我想确保单击按钮(图像或文本)按钮的背景颜色变为永久(第二张图像)。
我不喜欢按下按钮的效果,我只想要按钮的背景颜色。我可以通过将图像 + 文本合并为一个选择来修改我创建的按钮吗?或者您是否必须以其他方式创建按钮?如果是这样,你能告诉我怎么做吗?我在网上找到了一些按钮,但它们不是我想要的:我不喜欢它看起来像一个按钮(我不想要矩形轮廓,我不想要按下的效果)。我该怎么办?
代码:
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image
root = Tk()
root.title("xxxx")
root.geometry("1920x1080+0+0")
root.config(bg="#f0f0f0")
root.state("normal")
topbar=Frame(root, width=2200, height=43, background="#e10a0a")
topbar.place(x=0, y=0)
leftbar=Frame(root, width=250, height=1400, background="white")
leftbar.place(x=1, y=44)
button1= tk.PhotoImage(file="/image.png")
btn1 = tk.Button(root, image=button1, borderwidth=0, highlightthickness=0, bg="white", text="sdsdsd", foreground='green')
btn1.place(x=2, y=43)
text1 = Label(root, text="aaaaa", bg="white", foreground='black', font='Verdana 12')
text1.place(x=70, y=58)
button2= tk.PhotoImage(file="/image.png")
btn2 = tk.Button(root, image=button2, borderwidth=0, highlightthickness=0, bg="white", text="sdsdsd", foreground='green')
btn2.place(x=2, y=100)
text2 = Label(root, text="bbbbbb", bg="white", foreground='black', font='Verdana 12')
text2.place(x=70, y=120)
更新 1
使用 compound 后,我明白了。我不知道这是否是最好的解决方案。我走近了,但我仍然没有我需要的东西。我想将选择颜色拉伸到白色矩形的末尾,然后我想永久为按钮的背景着色
更新 2
通过应用 width = 224,我修复了按钮背景颜色长度问题,但按钮居中。现在他的左边有 space,之前它不在那里。我想要它在左边的起始位置
您可以通过设置 compound
选项将 image
和 text
合并到一个按钮中。
还建议使用 pack()
而不是 place()
来为您的案例布置框架和按钮。
而且我认为这些按钮的 parent
应该是 leftbar
而不是 root
。
修改后的代码如下:
from tkinter import messagebox
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image
root = tk.Tk()
root.title("xxxx")
root.geometry("1920x1080+0+0")
root.config(bg="#f0f0f0")
root.state("normal")
topbar = tk.Frame(root, background="#e10a0a", height=43)
topbar.pack(fill='x') # use pack() instead of place()
leftbar = tk.Frame(root, width=250, background="white")
leftbar.pack(side='left', fill='y') # use pack() instead of place()
leftbar.pack_propagate(0) # disable size auto-adjustment
def clicked(btn):
for w in leftbar.winfo_children():
w['bg'] = 'white' if w is not btn else 'yellow'
button1 = tk.PhotoImage(file="/image.png")
btn1 = tk.Button(leftbar, image=button1, borderwidth=0, highlightthickness=0,
bg="white", text="aaaaa", foreground='green', compound='left', anchor='w')
btn1.pack(fill='x') # use pack() instead of place()
btn1['command'] = lambda: clicked(btn1)
button2 = tk.PhotoImage(file="/image.png")
btn2 = tk.Button(leftbar, image=button2, borderwidth=0, highlightthickness=0,
bg="white", text="bbbbb", foreground='green', compound='left', anchor='w')
btn2.pack(fill='x') # use pack() instead of place()
btn2['command'] = lambda: clicked(btn2)
root.mainloop()
输出:
我构建了一个按钮图像(第一张图像),但我手动将相邻文本添加为纯文本。我想确保单击按钮(图像或文本)按钮的背景颜色变为永久(第二张图像)。
我不喜欢按下按钮的效果,我只想要按钮的背景颜色。我可以通过将图像 + 文本合并为一个选择来修改我创建的按钮吗?或者您是否必须以其他方式创建按钮?如果是这样,你能告诉我怎么做吗?我在网上找到了一些按钮,但它们不是我想要的:我不喜欢它看起来像一个按钮(我不想要矩形轮廓,我不想要按下的效果)。我该怎么办?
代码:
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image
root = Tk()
root.title("xxxx")
root.geometry("1920x1080+0+0")
root.config(bg="#f0f0f0")
root.state("normal")
topbar=Frame(root, width=2200, height=43, background="#e10a0a")
topbar.place(x=0, y=0)
leftbar=Frame(root, width=250, height=1400, background="white")
leftbar.place(x=1, y=44)
button1= tk.PhotoImage(file="/image.png")
btn1 = tk.Button(root, image=button1, borderwidth=0, highlightthickness=0, bg="white", text="sdsdsd", foreground='green')
btn1.place(x=2, y=43)
text1 = Label(root, text="aaaaa", bg="white", foreground='black', font='Verdana 12')
text1.place(x=70, y=58)
button2= tk.PhotoImage(file="/image.png")
btn2 = tk.Button(root, image=button2, borderwidth=0, highlightthickness=0, bg="white", text="sdsdsd", foreground='green')
btn2.place(x=2, y=100)
text2 = Label(root, text="bbbbbb", bg="white", foreground='black', font='Verdana 12')
text2.place(x=70, y=120)
更新 1
使用 compound 后,我明白了。我不知道这是否是最好的解决方案。我走近了,但我仍然没有我需要的东西。我想将选择颜色拉伸到白色矩形的末尾,然后我想永久为按钮的背景着色
更新 2
通过应用 width = 224,我修复了按钮背景颜色长度问题,但按钮居中。现在他的左边有 space,之前它不在那里。我想要它在左边的起始位置
您可以通过设置 compound
选项将 image
和 text
合并到一个按钮中。
还建议使用 pack()
而不是 place()
来为您的案例布置框架和按钮。
而且我认为这些按钮的 parent
应该是 leftbar
而不是 root
。
修改后的代码如下:
from tkinter import messagebox
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image
root = tk.Tk()
root.title("xxxx")
root.geometry("1920x1080+0+0")
root.config(bg="#f0f0f0")
root.state("normal")
topbar = tk.Frame(root, background="#e10a0a", height=43)
topbar.pack(fill='x') # use pack() instead of place()
leftbar = tk.Frame(root, width=250, background="white")
leftbar.pack(side='left', fill='y') # use pack() instead of place()
leftbar.pack_propagate(0) # disable size auto-adjustment
def clicked(btn):
for w in leftbar.winfo_children():
w['bg'] = 'white' if w is not btn else 'yellow'
button1 = tk.PhotoImage(file="/image.png")
btn1 = tk.Button(leftbar, image=button1, borderwidth=0, highlightthickness=0,
bg="white", text="aaaaa", foreground='green', compound='left', anchor='w')
btn1.pack(fill='x') # use pack() instead of place()
btn1['command'] = lambda: clicked(btn1)
button2 = tk.PhotoImage(file="/image.png")
btn2 = tk.Button(leftbar, image=button2, borderwidth=0, highlightthickness=0,
bg="white", text="bbbbb", foreground='green', compound='left', anchor='w')
btn2.pack(fill='x') # use pack() instead of place()
btn2['command'] = lambda: clicked(btn2)
root.mainloop()
输出: