【问题标题】:Can I define a function to create Tkinter widgets inside a class?我可以定义一个函数来在类中创建 Tkinter 小部件吗?
【发布时间】:2019-08-31 20:30:35
【问题描述】:

我创建了一个类来定义 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'

【问题讨论】:

  • button_gen 缺少 self 关键字。也将它移动到一些函数中
  • stovfl - 明确地说,您是在建议一个继承超类实例的子类吗?

标签: python function oop inheritance tkinter


【解决方案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,它定义了类内所有Buttonall通用参数。

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

【讨论】:

  • 谢谢。你为我节省了很多我没有的时间。 :)
猜你喜欢
  • 2015-02-06
  • 1970-01-01
  • 2019-11-28
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
相关资源
最近更新 更多