从Tk类继承Python

2024-10-03 17:18:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个模块化类(用于一些gui按钮)。 CoreButton应该包含一个公共按钮的大多数方法,包括tk frame。在

目标是嵌入CoreButton-并使用其框架来构建按钮的GUI的其余部分-它不会出现。在

任何帮助都会得到认可

class CoreButton(ttk.Frame):

    def __init__(self, master,nickname, hw_in=[], hw_out=[],ip_in='', ip_out='', sched_vector=[]):
        ttk.Frame.__init__(self, master)
        self.master = master
        if ip_in == '': ip_in = ip_out  # in case remote input is not defined

        self.grid()
     #####Rest of code

以及继承的类:

^{pr2}$

Tags: 方法inselfipmasterinitgui模块化
1条回答
网友
1楼 · 发布于 2024-10-03 17:18:11

我不知道你想做什么,但我会做这样的事

我不在类内使用self.grid(),所以在类外我可以使用tb1.pack()或{},这取决于我在窗口中使用的布局管理器。在

__init__中,我执行self.build_gui(),因此不必手动执行,但现在所有类都必须创建self.build_gui()而不带参数。在

我添加Label仅用于测试-显示“已选择”/“未选择”。你不需要它。在

import tkinter as tk
from tkinter import ttk

class CoreButton(ttk.Frame):

    def __init__(self, master, nickname, hw_in=None, hw_out=None, ip_in=None, ip_out=None, sched_vector=None):
        ttk.Frame.__init__(self, master)

        self.nickname = nickname

        self.hw_in = hw_in
        if self.hw_in is None:
            self.hw_in = []

        #self.hw_in = hw_in or []

        self.hw_out = hw_out
        if self.hw_out is None:
            self.hw_out = []

        #self.hw_out = hw_out or []

        self.ip_out = ip_out
        self.ip_in = ip_in
        if self.ip_in is None:
            self.ip_in = self.ip_out  # in case remote input is not defined

        #self.ip_in = hw_in or self.ip_out

        self.sched_vector = sched_vector
        if sched_vector is None:
            sched_vector = []

        #self.sched_vector = sched_vector or []

        self.build_gui() # < - to build it automatically

    def build_gui(self):
        # you will overide it in child widgets
        raise NotImplementedError('You have to override method build_gui()')


class ToggleBut2(CoreButton):

    def __init__(self, master, hw_in=None, hw_out=None, ip_in=None, ip_out=None, sched_vector=None, height=3, width=13):

        self.height = height
        self.width = width

        # `self.but_var` is used in `build_gui` so it has to be created before `__init__` which executes `build_gui`
        # or create it directly in `build_gui`
        #self.but_var = tk.StringVar()

        CoreButton.__init__(self, master, "JOHM", hw_in, hw_out, ip_in, ip_out, sched_vector)


    def build_gui(self, nickname='babe'):
        self.but_var = tk.IntVar()

        self.button = tk.Checkbutton(self, text=self.nickname, variable=self.but_var, indicatoron=0, height=self.height, width=self.width, command=self.sf_button_press)
        self.button.grid(row=0, column=0)

        self.label = tk.Label(self, text='[not selected]')
        self.label.grid(row=1, column=0)


    def sf_button_press(self):
        print(self.but_var.get())
        if self.but_var.get() == 0:
            self.label['text'] = '[ not selected ]'
        else:
            self.label['text'] = '[  selected  ]'

#  - main  -


root = tk.Tk()

tb1 = ToggleBut2(root, height=1, width=10)
tb1.pack()

tb2 = ToggleBut2(root, height=3, width=30)
tb2.pack()

tb2 = ToggleBut2(root, height=5, width=50)
tb2.pack()

root.mainloop()

enter image description here

相关问题 更多 >