我想为我的脚本使用tkinter得到舍入按钮。圆形按钮tkinter python
我发现下面的代码:
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()
,但我得到了以下错误:
TypeError: __init__() missing 4 required positional arguments: 'parent', 'width', 'height', and 'color'
谢谢。这使它运行,但按钮不是圆的 –
不,它不是。但是,这正是您“发现”的代码应该做的事情。它使长方形帆布凸起浮雕,并绘制一个椭圆形。当您按下/释放按钮时,会使凹陷再次凹陷/抬起。 – avysk