estoy tratando de hacer una calculadora con GUI y en este momento estoy tratando de hacer que cuando se presione un botón este cambié su relief
, estoy tratando de usarlo pero el intérprete me arroja este error.
a = tk.Button(self, text="1", command=lambda: a.config(relief=GROOVE), relief=FLAT,
AttributeError: 'NoneType' object has no attribute 'config'
No sé si adjuntar el botón a una variable es la mejor forma de hacerlo:
a = tk.Button(self, text="1", command=lambda: a.config(relief=GROOVE), relief=FLAT,
font=myfont, bg="ghost white").grid(column=0, row=1, sticky="NSEW", padx=3, pady=3, ipadx=0,
ipady=0)
Traté de hacerlo así también:
tk.Button(self, text="1", command=lambda: self.config(relief=GROOVE), relief=FLAT,
font=myfont, bg="ghost white").grid(column=0, row=1, sticky="NSEW", padx=3, pady=3, ipadx=0,
ipady=0)
Pero al parecer lo que hace es modificar el objeto self
(el cual es el frame en el que están los botones)
Espero que me puedan ayudar
(Aquí está todo el código)
from Calculator import *
import tkinter as tk
from tkinter import *
from tkinter.font import Font
from PIL import Image, ImageTk
def operation_to_do(symbol):
op = ""
if symbol == "+":
op = "Plus"
elif symbol == "-":
op = "Subtract"
elif symbol == "X":
op = "Multiplication"
elif symbol == "/":
op = "Divide"
elif symbol == "C":
pass
elif symbol == "CE":
pass
return op
def call_function_button(self, variable, entry):
self.configure(text="Hello")
insert_number(variable, entry)
def insert_number(variable, entry):
result = variable + entry
return result
def operation():
pass
def transform_conjunt(conjunt):
value = int(conjunt)
return value
conjunt = ""
conjunt2 = ""
class MainFrame(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.grid()
self.config(bg="white")
def change_button():
pass
myfont = Font(family="Arial", size=15)
self.i = Image.open("C:/Users/Darwin Lozada/Desktop/1.png")
self.i2 = self.i.resize((100, 100))
self.image1 = ImageTk.PhotoImage(self.i2)
tk.Label(self, text="TEST", font=myfont).grid(column=0, row=0, sticky="NSEW", columnspan=3, padx=2.5, pady=2.5)
# Numbers Buttons
tk.Button(self, text="1", command=lambda: self.config(relief=GROOVE), relief=FLAT,
font=myfont, bg="ghost white").grid(column=0, row=1, sticky="NSEW", padx=3, pady=3, ipadx=0,
ipady=0)
tk.Button(self, text="2", font=myfont,
relief=FLAT, activebackground="gainsboro", bg="ghost white").grid(column=1, row=1, sticky="NSEW",
padx=3,
pady=3)
tk.Button(self, text="3", command=lambda: call_function_button(self, conjunt, "3"), font=myfont,
relief=FLAT, bg="ghost white").grid(column=2, row=1, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="4", command=lambda: call_function_button(self, conjunt, "4"), font=myfont,
relief=FLAT, bg="ghost white").grid(
column=0, row=2, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="5", command=lambda: call_function_button(self, conjunt, "5"), font=myfont,
relief=FLAT, bg="ghost white").grid(
column=1, row=2, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="6", command=lambda: call_function_button(self, conjunt, "6"), font=myfont,
relief=FLAT, bg="ghost white").grid(
column=2, row=2, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="7", command=lambda: call_function_button(self, conjunt, "7"), font=myfont,
relief=FLAT, bg="ghost white").grid(
column=0, row=3, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="8", command=lambda: call_function_button(self, conjunt, "8"), font=myfont,
relief=FLAT, bg="ghost white").grid(
column=1, row=3, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="9", command=lambda: call_function_button(self, conjunt, "9"), font=myfont,
relief=FLAT, bg="ghost white").grid(
column=2, row=3, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="0", command=lambda: call_function_button(self, conjunt, "0"), font=myfont,
relief=FLAT, bg="ghost white").grid(
column=1, row=4, sticky="NSEW", padx=3, pady=3)
# Operations Buttons
tk.Button(self, text="+", font=myfont, command=lambda: operation_to_do("+"), relief=FLAT).grid(
column=3, row=0, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="-", font=myfont, command=lambda: operation_to_do("-"), relief=FLAT).grid(
column=3, row=1, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="X", font=myfont, command=lambda: operation_to_do("X"), relief=FLAT).grid(
column=3, row=2, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="/", font=myfont, command=lambda: operation_to_do("/"), relief=FLAT).grid(
column=3, row=3, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="C", font=myfont, command=lambda: operation_to_do("C"), relief=FLAT).grid(
column=0, row=4, sticky="NSEW", padx=3, pady=3)
tk.Button(self, text="CE", font=myfont, command=lambda: operation_to_do("CE"), relief=FLAT).grid(
column=2, row=4, sticky="NSEW", padx=3, pady=3)
# Result Button
tk.Button(self, text="=", font=myfont, relief=FLAT).grid(column=3, row=4, sticky="NSEW", padx=3, pady=3)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(2, weight=1)
self.columnconfigure(3, weight=1)
self.rowconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.rowconfigure(2, weight=1)
self.rowconfigure(3, weight=1)
self.rowconfigure(4, weight=1)
def give_result(self):
pass
def main():
root = tk.Tk()
root.title("Calculadora")
root.minsize(300, 350)
buttons_frame = MainFrame(root)
buttons_frame.grid(row=0, column=0, sticky="NSEW")
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.mainloop()
if __name__ == "__main__":
main()
call_function_button
la hice con esa intención pero tampoco sirvió