Home > Back-end >  How to use a padding to rounded button on tkinter
How to use a padding to rounded button on tkinter

Time:09-17

I want a rounded button like on the image

button rounded

and change de color of the background the button without class (I don't know how to use a class if you teach me I can use it too) with python tkinter. I have tried

button = CustomButton(window, 100, 25, 'red') 

and I get an error NameError: name 'CustomButton' is not defined

This is my code

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()

CodePudding user response:

The problem is, that you are using a class created by someone else and did not include it in your code. The CustomButton-class needs to be included on the top of your file.

I would really urge you to read up on classes. Object-oriented-programming is the basis of python and how to write more complex code.

CodePudding user response:

You need to include that class in your file or in a separate file and then import it.

here is an example (The rounded button is taken from enter image description here

  • Related