Rounded button tkinter python

前端 未结 4 964
无人共我
无人共我 2021-02-13 03:23

I am trying to get rounded buttons for my script using tkinter.

I found the following code:

from tkinter import *
import tkinter as tk

class CustomButto         


        
4条回答
  •  不思量自难忘°
    2021-02-13 04:08

    I made this rounded rectangle button if anyone was looking for more of an apple look or something. For convenience here are the arguments:

    RoundedButton(parent, width, height, cornerradius, padding, fillcolor, background, command)
    

    Note: If the corner radius is greater than half of the width or height an error message will be sent in the terminal. Pill shapes can still be made through if you set the corner radius to exactly half of the height or width.

    Finally the code:

    from tkinter import *
    import tkinter as tk
    
    root = Tk()
    
    class RoundedButton(tk.Canvas):
        def __init__(self, parent, width, height, cornerradius, padding, color, bg, command=None):
            tk.Canvas.__init__(self, parent, borderwidth=0, 
                relief="flat", highlightthickness=0, bg=bg)
            self.command = command
    
            if cornerradius > 0.5*width:
                print("Error: cornerradius is greater than width.")
                return None
    
            if cornerradius > 0.5*height:
                print("Error: cornerradius is greater than height.")
                return None
    
            rad = 2*cornerradius
            def shape():
                self.create_polygon((padding,height-cornerradius-padding,padding,cornerradius+padding,padding+cornerradius,padding,width-padding-cornerradius,padding,width-padding,cornerradius+padding,width-padding,height-cornerradius-padding,width-padding-cornerradius,height-padding,padding+cornerradius,height-padding), fill=color, outline=color)
                self.create_arc((padding,padding+rad,padding+rad,padding), start=90, extent=90, fill=color, outline=color)
                self.create_arc((width-padding-rad,padding,width-padding,padding+rad), start=0, extent=90, fill=color, outline=color)
                self.create_arc((width-padding,height-rad-padding,width-padding-rad,height-padding), start=270, extent=90, fill=color, outline=color)
                self.create_arc((padding,height-padding-rad,padding+rad,height-padding), start=180, extent=90, fill=color, outline=color)
    
    
            id = shape()
            (x0,y0,x1,y1)  = self.bbox("all")
            width = (x1-x0)
            height = (y1-y0)
            self.configure(width=width, height=height)
            self.bind("", self._on_press)
            self.bind("", 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()
    
    def test():
        print("Hello")
    
    canvas = Canvas(root, height=300, width=500)
    canvas.pack()
    
    button = RoundedButton(root, 200, 100, 50, 2, 'red', 'white', command=test)
    button.place(relx=.1, rely=.1)
    
    root.mainloop()
    

提交回复
热议问题