我可以定义一个函数在类内创建Tkinter小部件吗?

时间:2019-04-10 11:36:00

标签: python function oop inheritance tkinter

我创建了一个类来定义Tkinter窗口及其小部件。每个按钮都带有几个参数作为选项。我想在类中定义一个函数,而不是复制并粘贴创建和定位按钮的两行代码。之后,我将在调用时将选项值作为参数传递,并快速创建新按钮。

from tkinter import *

class Window:
    def __init__(self, master):
        self.master = master
        master.title("UDP")

        self.label = Label(master, text="Window1")
        self.label.place(relx=0.9, rely=0.9)

        self.canvas = Canvas(master,
                             bg="red",
                             width=1160,
                             height=772
                             )
        self.canvas.grid()

        self.image = PhotoImage(file="E:\\Python\\world_map.png")
        self.canvas.create_image(0, 0, anchor=NW, image=self.image)

        self.one = Button(master, 
                          text="1", 
                          command=self.one, 
                          bg="red", bd=8, 
                          height=2, 
                          width=10, 
                          activebackground="blue")
        self.one.place(relx=0.6, 
                       rely=0.9)

        self.two = Button(master, 
                          text="2", 
                          command=self.two, 
                          bg="red",
                          bd=8, 
                          height=2, 
                          width=10, 
                          activebackground="blue")
        self.two.place(relx=0.7, 
                       rely=0.9)


        self.three = Button(master, 
                            text="3", 
                            command=self.three, 
                            bg="red", 
                            bd=8, 
                            height=2, 
                            width=10, 
                            activebackground="blue")
        self.three.place(relx=0.8, 
                         rely=0.9)

        self.close_button = Button(master, 
                                   text="Quit", 
                                   command=master.quit, 
                                   bg='red', 
                                   bd=8, 
                                   height=2, 
                                   width=10, 
                                   activebackground="blue")
        self.close_button.place(relx=0.9, 
                                rely=0.9)

class button(Window):
    def __init__(self):
        super(Window, self).__init__()
    def button_gen(self, x, y, z, a, b, c, d, e):
        self.one = Button(self, text=x,
                    command=self.one,
                    bg=y,
                    bd=z,
                    height=a,
                    width=b,
                    activebackground=c)
        self.one.place(relx=d,
                       rely=e)

    button_gen(
               "4",
               "red",
               8,
               2,
               10,
               "blue",
               0.1,
               0.2
                )

    def one(self):
        print("1")
    def two(self):
        print("2")
    def three(self):
        print("3")



root = Tk()
lbl = Label(root, text="5")
my_gui = Window(root)
root.mainloop()

但是我遇到以下错误:

Traceback (most recent call last):
  File "E:/Python/Tkinter.py", line 34, in <module>
    class button(Window):
  File "E:/Python/Tkinter.py", line 56, in button
    0.2
TypeError: button_gen() missing 1 required positional argument: 'e'

1 个答案:

答案 0 :(得分:1)

  

问题:继承自tkinter小部件

使用从tkinter.Button小部件继承的自己的小部件对象。


而不是重复常见的参数,例如:

    self.one = Button(master, 
                      text="1", 
                      command=self.one, 
                      bg="red", bd=8, 
                      height=2, 
                      width=10, 
                      activebackground="blue")
    self.one.place(relx=0.6, 
                   rely=0.9)

定义您自己的class MyButton,它为类中的所有Button定义了 all 通用参数。

class MyButton(tk.Button): 
    def __init__(self, parent, relx, rely, **kwargs): 
        super().__init__(parent, kwargs,
                         bg="red", bd=8, height=2, width=10, activebackground="blue")

        self.place(relx=relx, rely=rely)
  

用法

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.one = MyButton(self, 0.6, 0.9, text="1", command=self.one)
        self.two = MyButton(self, 0.7, 0.9, text="2", command=self.two)
        self.three = MyButton(self, 0.8, 0.9, text="3", command=self.three)

        # Alternative
        relx = 0.6
        for cfg in [("1", self.one), ("2", self.two), ("3", self.three)]:
            MyButton(self, relx, 0.9, text=cfg[0], command=cfg[1])
            relx += 0.1

if __name__ == "__main__":
    App().mainloop()

使用Python测试:3.5