Python - 从Tk类继承

时间:2017-12-16 12:45:27

标签: python inheritance tkinter

我正在尝试创建一个模块化类(对于一些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

和继承的类:

class ToggleBut2(CoreButton):
    def __init__(self, master, hw_in=[], hw_out=[],ip_in='', ip_out='', sched_vector=[]):
        CoreButton.__init__(self, master, nickname="JOHM", hw_in=hw_in, hw_out=hw_out, ip_in=ip_in, ip_out=ip_out, sched_vector=sched_vector)
        self.master = master

    def build_gui(self, nickname='babe', height=3, width=13):

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

1 个答案:

答案 0 :(得分:2)

我不知道你尝试做什么,但我会做这样的事情

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

__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