0

I want to create a custom class for the tkinter button class. However when I run my code the button doesn't appear on my window. The programme doesn't throw any errors either. Running on Mac on python 3.9.

import tkinter, sys
from tkinter import *

class CustomButton(Button):
    def __init__(self, parent, button_text):
        Button.__init__(self, master = parent)
        self.text = button_text

class Screen(tkinter.Tk):
    def __init__(self):
        super().__init__()
        self.title("Button Test")
        self.geometry("300x300")
        self.new_button = CustomButton(self, "Button One")
        self.new_button.pack()
        self.protocol("WM_DELETE_WINDOW", self.quit)

    def quit(self):
        sys.exit(0)

if __name__ == '__main__':
    app = Screen()
    app.mainloop()

I tried positioning the button using .pack() and .grid() but neither yielded any results.

2
  • Did you get a button without using class? If not, it is the issue on your Python installatioin. Try reinstalling Python from python.org. Note also that setting the attribute .text does not set the text of the button. You need to set the text option in Button.__init__(...). Commented May 27, 2024 at 0:51
  • @acw1668 Setting the text in the Button.__init__() worked. Thanks. Commented May 27, 2024 at 2:36

1 Answer 1

0

For Mac.

from tkinter import *
from tkmacosx import Button
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.