繁体   English   中英

Python Tkinter,更改特定行背景颜色?

[英]Python Tkinter, change specific row background colour?

我已经能够通过这个改变整个窗口和特定标签的背景颜色,

master.configure(background = 'SteelBlue1')
titlelabel = Tkinter.Label(master, text="my Title", fg = "blue4", bg = "gray80").grid(row=0, column = 1)

有没有一种简单的方法可以使第 0 行变为灰色,而不仅仅是标签周围的区域? 谢谢

如果此行上只有标题,则可以使用columspan ,使标题跨所有列,并水平扩展标签:

import Tkinter

master = Tkinter.Tk()

master.configure(background='SteelBlue1')
master.columnconfigure(0, weight=1) # make the column 1 expand when the window is resized
nb_of_columns = 2 # to be replaced by the relevant number
titlelabel = Tkinter.Label(master, text="my Title", fg="blue4", bg ="gray80")
titlelabel.grid(row=0, column=0, sticky='ew', columnspan=nb_of_columns) # sticky='ew' expands the label horizontally

master.geometry('200x200')
master.mainloop()

否则,解决方案是遵循scotty3785的建议并使用框架:

import Tkinter

master = Tkinter.Tk()

master.configure(background='SteelBlue1')
master.columnconfigure(1, weight=1)

nb_of_columns = 2 # to be replaced by the relevant number
titleframe = Tkinter.Frame(master, bg ="gray80")
titleframe.grid(row=0, column=0, columnspan=nb_of_columns, sticky='ew')
titlelabel = Tkinter.Label(titleframe, text="my Title", fg="blue4", bg ="gray80")
titlelabel.grid(row=0, column=1)
# other widgets on the same row:
Tkinter.Button(titleframe, text='Ok').grid(row=0, column=2)

master.geometry('200x200')
master.mainloop()

我想出了一种不使用 Frame 的方法,这在我的实现中是不可行的。 您必须在网格上使用sticky=tk.EW 和在标签上对齐/锚定的组合。

这是我的一段代码,它在网格中列出了帐户名称、余额和帐户类型。 当您将鼠标悬停在其中一行上时,整行将突出显示而没有任何间隙。 唯一使用的框架是保存所有内容(我需要保持列整齐有序)——它不为每一行使用一个框架。

import tkinter as tk
from tkinter import ttk

def show_accounts(self) -> None:
    account_frame = ttk.Frame(self.content_frame)
    account_frame.grid(column=0, row=0)

    ttk.Label(account_frame, text="Account").grid(column=0, row=0, sticky=tk.W, padx=(0,20))
    ttk.Label(account_frame, text="Balance").grid(column=1, row=0, sticky=tk.W, padx=(0,20))
    ttk.Label(account_frame, text="Type").grid(column=2, row=0, sticky=tk.W)
    ttk.Separator(account_frame).grid(column=0, row=1, sticky=tk.EW, columnspan=3)

    accounts = self.controller.get_accounts()
    
    for i, account in enumerate(accounts):
        account_name = ttk.Label(account_frame, text=str(account.name), justify=tk.LEFT, anchor=tk.W)
        account_name.grid(column=0, row=i+2, sticky=tk.EW, ipadx=20)

        account_balance = ttk.Label(account_frame, text=f"{account.balance:,.2f}", justify=tk.LEFT, anchor=tk.W)
        account_balance.grid(column=1, row=i+2, sticky=tk.EW, ipadx=20)
        
        account_type = ttk.Label(account_frame, text=AccountType(account.type).name.title(), justify=tk.LEFT, anchor=tk.W)
        account_type.grid(column=2, row=i+2, sticky=tk.EW)

        # Bindings
        account_name.bind("<Enter>", lambda e, account=account: self.highlight_account_row(e, account))
        account_name.bind("<Leave>", lambda e, account=account: self.unhighlight_account_row(e, account))

        # Save to dictionary
        self.widgets[f"{account.name}_name"] = account_name
        self.widgets[f"{account.name}_balance"] = account_balance
        self.widgets[f"{account.name}_type"] = account_type

def highlight_account_row(self, event, account):
    self.widgets[f"{account.name}_name"].configure(background="grey")
    self.widgets[f"{account.name}_balance"].configure(background="grey")
    self.widgets[f"{account.name}_type"].configure(background="grey")

def unhighlight_account_row(self, event, account):
    self.widgets[f"{account.name}_name"].configure(background="white")
    self.widgets[f"{account.name}_balance"].configure(background="white")
    self.widgets[f"{account.name}_type"].configure(background="white")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM