ttk.Widget自定义条目,该条目不可编辑或不可点击

问题描述 投票:-1回答:1

这里是一些代码:

import tkinter.ttk as _ttk
import tkinter as _tk

class QEntry(_ttk.Widget):
    def __init__(self, master):
        super(QEntry, self).__init__(master, "ttk::entry", {"class": "QEntry", "style": "QEntry"})


def init(widget=None):
    flat = "flat"

    s = _ttk.Style()
    s.theme_create("QUI", "default")
    s.theme_use("QUI")
    s.layout("QEntry", [('Entry.highlight', {'border': 0,'sticky': 'nswe','children': [('Entry.border', {'border': 0,'sticky': 'nswe','children': [('Entry.padding', {'sticky': 'nswe','children': [('Entry.textarea', {'sticky': 'nswe','border': 0})]})]}), ('Entry.bd', {'sticky': 'nswe','border': 0,'children': [('Entry.padding', {'sticky': 'nswe','children': [('Entry.textarea', {'sticky': 'nswe'})]})]})]})])
    s.theme_settings("QUI", {"QEntry": {"configure": {"padding": 4},"map": {"relief": [("active", flat),("focus", flat),("!disabled", flat),("disabled", flat)],"background": [("active", "#00afaf"),("focus", "#6f6f6f"),("!disabled", "#6f6f6f"),("disabled", "#8f8f8f")],"bordercolor": [("active", "#00afaf"),("focus", "#00afaf"),("!disabled", "#6f6f6f"),("disabled", "#5f5f5f")],"foreground": [("active", "#ffffff"),("focus", "#ffffff"),("!disabled", "#afafaf"),("disabled", "#5f5f5f")],}}})


if __name__ == '__main__':
    root = _tk.Tk()
    root.wm_minsize(200, 150)
    init(root)

    frame = _tk.Frame(root)
    entry = QEntry(frame)
    entry.pack(pady=1)
    frame.pack(fill="both", expand=True)

    root.mainloop()

该条目不可编辑或不可点击。为什么?QEntry(...)类是_ttk.Widget的子类。init(...)是样式/布局的初始化。

python-3.x tkinter ttk
1个回答
0
投票

我找到了,为此,您只需要将class中的TEntry键的值更改为class QEntry(_ttk.Widget):

import tkinter.ttk as _ttk
import tkinter as _tk

class QEntry(_ttk.Widget):
    def __init__(self, master):
        super(QEntry, self).__init__(master, "ttk::entry", {"class": "TEntry", "style": "QEntry"})


def init(widget=None):
...
© www.soinside.com 2019 - 2024. All rights reserved.