我想爲我的腳本使用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