Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
5 views
in Python by (16.4k points)
closed by

With the help of tkinter, For my script, I'm attempting to get the rounded buttons.

I found the accompanying code:

from tkinter import *

import tkinter as tk

class CustomButton(tk.Canvas):

    def __init__(self, parent, width, height, color, command=None):

        tk.Canvas.__init__(self, parent, borderwidth=1, 

            relief="raised", highlightthickness=0)

        self.command = command

        padding = 4

        id = self.create_oval((padding,padding,

            width+padding, height+padding), outline=color, fill=color)

        (x0,y0,x1,y1)  = self.bbox("all")

        width = (x1-x0) + padding

        height = (y1-y0) + padding

        self.configure(width=width, height=height)

        self.bind("<ButtonPress-1>", self._on_press)

        self.bind("<ButtonRelease-1>", self._on_release)

    def _on_press(self, event):

        self.configure(relief="sunken")

    def _on_release(self, event):

        self.configure(relief="raised")

        if self.command is not None:

            self.command()

app = CustomButton()

app.mainloop()

But, I also the following error:

TypeError: __init__() missing 4 required positional arguments: 'parent', 'width', 'height', and 'color'

closed

5 Answers

0 votes
by (19k points)
 
Best answer

To resolve the TypeError in your code and display rounded buttons using Tkinter, make the following modifications:

  1. Create a Tkinter root window: root = tk.Tk()

  2. Instantiate the CustomButton class with the required arguments: app = CustomButton(root, width=100, height=50, color="red")

  3. Add the button to the root window: app.pack()

  4. Start the Tkinter event loop: root.mainloop()

With these changes, you can fix the error and display the desired rounded buttons.

0 votes
by (26.4k points)

An extremely simple approach to create a rounded button in tkinter is to utilize a picture. 

To begin with, make a picture of what you need your button to resemble save it as a .png and eliminate the external foundation so it is adjusted like the one underneath:

Next, embed the picture in a catch/button with PhotoImage like this: 

self.loadimage = tk.PhotoImage(file="rounded_button.png")

self.roundedbutton = tk.Button(self, image=self.loadimage)

self.roundedbutton["bg"] = "white"

self.roundedbutton["border"] = "0"

self.roundedbutton.pack(side="top")

Guarantee to utilize border="0" and the button border will be taken out. 

I added the self.roundedborder["bg"] = "white" so the foundation the foundation of the button is equivalent to the Tkinter window.

Interested to learn python in detail? Come and Join the python course.

0 votes
by (25.7k points)
To resolve the TypeError and fix the error in the provided code, you need to pass the required arguments when creating an instance of the CustomButton class. Modify the last lines of code as shown below:

root = tk.Tk()  # Create a Tkinter root window

app = CustomButton(root, width=100, height=50, color="red")  # Pass the required arguments

app.pack()  # Pack the button into the root window

root.mainloop()  # Start the Tkinter event loop

Make sure to adjust the values for width, height, and color as desired. This updated code creates a root window, creates an instance of the CustomButton class with the required arguments, packs the button into the root window, and starts the Tkinter event loop.
0 votes
by (15.4k points)
To fix the TypeError and resolve the error in the provided code, make the following changes:

root = tk.Tk()  # Create a Tkinter root window

app = CustomButton(root, width=100, height=50, color="red")  # Provide the necessary arguments

app.pack()  # Add the button to the root window

root.mainloop()  # Start the Tkinter event loop

Ensure that you adjust the values of width, height, and color according to your requirements. This updated code creates a root window using Tkinter, instantiates the CustomButton class with the required arguments, adds the button to the root window, and initiates the Tkinter event loop.
0 votes
by (19k points)
To fix the TypeError in the provided code and resolve the error, follow these steps:

Create a Tkinter root window:

root = tk.Tk()

Instantiate the CustomButton class with the required arguments (parent, width, height, and color):

app = CustomButton(root, width=100, height=50, color="red")

Adjust the values of width, height, and color as needed.

Add the button to the root window:

app.pack()

Start the Tkinter event loop to display the button and handle user interactions:

root.mainloop()

By following these steps, you can resolve the TypeError and successfully run the code to display rounded buttons using Tkinter.

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...