Пользовательская кнопка Tkinter не появляется - PullRequest
0 голосов
/ 23 апреля 2020

Я пытаюсь создать пользовательскую кнопку tkinter для использования, но она не отображается в моем окне.

Класс кнопки (внутри скрипта с именем TKCustom)

import tkinter as tk
class Button(tk.Frame):
    def __init__(self, root, width=100, height=50, posx=0, posy=0, anchor="nw", text="Button", function=print("Button"), font=("Arial", 10)):
        self.root = root
        self.width = width
        self.height = height
        self.posx = posx
        self.posy = posy
        self.anchor = anchor
        self.text = tk.StringVar()
        self.text.set(text)
        self.function = function
        self.font = font
        tk.Frame.__init__(self, root, width=self.width, height=self.height)
        self.button = tk.Button(self, textvariable=self.text, font=self.font, command=self.function)
        self.button.pack(expand=tk.YES, fill=tk.BOTH)
        print("button created")

    def show(self, *args):
        print("showing button")
        tk.Frame.pack(self, padx=self.posx, pady=self.posy, anchor=self.anchor, *args)
        print("button shown")

Код, выполняющий класс кнопки (другой сценарий)

import tkinter as tk
import TKCustom as tkc

window = tk.Tk()
window.geometry("600x400")
window.resizable(False, False)
window.bind("<Escape>", lambda e: window.quit())

test_label = tkc.Label(window, width=200, height=50, posx=15, posy=15, text="Test 1234")
test_label.show()

test_button = tkc.Button(window, width=100, height=30, posx=50, posy=200, text="Test Button", function=test_label.config)
test_button.show()
window.mainloop()

Все операторы печати печатаются, ошибки не возвращаются, кнопка просто не отображается.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...