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.
.textdoes not set the text of the button. You need to set thetextoption inButton.__init__(...).