为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

呼唤远方 2022-01-05 20:12:14
我创建了一个类来定义 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)
查看完整描述

1 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

问题:从小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。


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


查看完整回答
反对 回复 2022-01-05
  • 1 回答
  • 0 关注
  • 213 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号