1

我想更改 ttk 组合框的边框宽度和浮雕。但我只想为 1 个组合框更改这些属性。有没有办法让第二个组合框的边框宽度为 3 和实心浮雕,如文本框 2?

import tkinter as tk
from tkinter import font as tkfont, filedialog, messagebox
from tkinter.ttk import Combobox

class SLS_v1(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # Setting up the root window
        self.title('APP')
        self.geometry("552x700")
        self.resizable(False, False)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        self.frames["MenuPage"] = MenuPage(parent=container, controller=self)


        self.frames["MenuPage"].grid(row=0, column=0, sticky="nsew")

        self.show_frame("MenuPage")


    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class MenuPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        text1 = tk.Text(self, width=25, height=1)
        text1.pack(pady=20)

        combobox1 = tk.ttk.Combobox(self, width=25, height=2, state='readonly')
        combobox1.pack(pady=20)

        text2 = tk.Text(self, width=50, borderwidth=3, relief='solid', height=1)
        text2.pack(pady=20)

        combobox2 = tk.ttk.Combobox(self, width=50, height=5, state='readonly')
        combobox2.pack(pady=20)


if __name__ == "__main__":
    app = SLS_v1()
    app.mainloop()
4

1 回答 1

1

要更改ttk对象,您必须像这样定义样式。

def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)

    self.style = ttk.Style(self)
    self.style.theme_use("default")

Combobox然后你可以为你的喜欢定义细节。

self.style.configure("K.TCombobox",
                     **dict(
                         padding = 1, arrowsize = 12,
                         borderwidth = 3, relief = "solid"))

像这样完成实现。

combobox2 = Combobox(self, width=50, height=5, state='readonly', style = "K.TCombobox")
于 2021-12-10T09:42:51.180 回答