簡體   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