亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我可以定義一個函數來在類中創建 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 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號