【发布时间】:2021-09-07 14:54:01
【问题描述】:
我想要一个像图片一样的圆形按钮
并更改按钮背景的颜色 没有课(如果你教我,我不知道如何使用课,我也可以使用它)与 python tkinter。 我试过了
button = CustomButton(window, 100, 25, 'red')
我收到一个错误NameError: name 'CustomButton' is not defined
这是我的代码
import tkinter as tk
from tkinter import *
from tkinter import font
#declarando a variavel da janela
window = tk.Tk()
#mudando o titulo da janela
window.title('sorteio')
#colocando o favicon na aba
window.iconbitmap("./img/pinguim.ico")
#pegando a resolução do monitor e colocando em uma variavel
widthValue = window.winfo_screenwidth()
heightValue = window.winfo_screenheight()
window.geometry("%dx%d+0+0" % (widthValue,heightValue))
#↑
#passando o valor de "%dx%d"
#deixando maximizada ao iniciar
window.state('zoomed')
#Define image
bg = PhotoImage(file="./img/background.png")
#criando um canvas
canvas1 = Canvas(window, width=widthValue, height=heightValue, bd=0 , highlightthickness=0)#highlightthickness= 0 tira a borda branca
canvas1.pack(fill="both", expand=True)
#colocando a imagem no canvas
canvas1.create_image(0,0, image=bg , anchor="nw")
#criando texto
canvas1.create_text(900,100, text="Sorteio", font=("Roboto", 25), fill="white", anchor="center")
canvas1.create_text(680,300, text=" Sortear entre:", font=("Roboto", 12), fill="white" )
inputMin = Entry(canvas1)
canvas1.create_window(800, 300, window=inputMin, height=25, width=120)
inputMin.insert(0, "minimo")
inputMin.config(fg="#808080", font="Roboto")
canvas1.create_text(885,300, text="e:", font=("Roboto",12), fill="white")
inputMax = Entry(canvas1)
inputMax.insert(0, "maximo")
inputMax.config(fg="#808080", font="Roboto")
canvas1.create_window(955, 300, window=inputMax, height=25, width=120)
#função
def sortear():
print("oi")
#primeiro cria o texto depois escolhe a posição dele
#textoSorteio = Label(window, text="Sorteio",font=("Roboto", 25), fg="white")#passando a janela pro texto
#textoSorteio.pack(pady=50)
button = CustomButton(window, 100, 25, 'red')
btnSorte = canvas1.create_oval()
btnSortear = Button(window, text="Sortear", width=15, height=1, pady=5,command=sortear)
btnSortear.place(x=1100, y=500)
window.mainloop()
【问题讨论】:
-
这是我的代码,如果你正在和
button = CustomButton(window, 100, 25, 'red')交谈,我会进入这里 stackoverflow.com/questions/42579927/… -
NameError出现是因为您尚未在脚本中定义CustomButton。尝试从您链接的问题中复制/粘贴课程。另外请看一些基本的python教程。
标签: python user-interface tkinter