私の意図は、いくつかのクラスからguiを作成し、私は小さな例を作成練習としてです。 appで見ることができるように、self.greetはButtonクラスからボタンを作成する必要がありますが、私が得るのは汎用ボタン(テキストとコールバック関数なし)です。 提案がありますか?tkinterクラスの構成が動作しません
import tkinter as tk
import tkinter.ttk as ttk
class Button(ttk.Button):
def __init__(self, parent):
super().__init__(parent)
self.item = ttk.Button(parent, text="Greet", command=self.greet)
def greet(self):
print("Greetings!")
class App():
def __init__(self, parent):
parent.title("A simple GUI")
self.label = ttk.Label(parent, text="A label")
self.greet = Button(parent)
self.button = ttk.Button(parent, text="Close", command=parent.quit)
self.label.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.W, tk.E))
self.greet.grid(row=1, column=0, sticky=(tk.N, tk.S, tk.W, tk.E))
self.button.grid(row=2, column=0, sticky=(tk.N, tk.S, tk.W, tk.E))
parent.grid_columnconfigure(0, weight=1)
def greet(self):
print("Greetings!")
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
_composition_を使用している場合は、 'ttk.Button'から継承するべきではありません。あなたのクラスは実際に2つのボタンを作成します。 –
私はコンセプトについて学んでいるので、私はできますし、私はものについて間違っています。私が 'class Button():'のような 'ttk.Button'を継承しないと、あなたのコメントに関して、' builtins.TypeError:object .__ init __()はパラメータを受け取りません '? – flebas