-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcontrol_ttk.py
More file actions
121 lines (95 loc) · 3.46 KB
/
pcontrol_ttk.py
File metadata and controls
121 lines (95 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# pcontrol_ttk.py
# -*- coding: utf-8 -*-
'''
Panel de control con ttk (Tkinter moderno)
Opciones implementadas:
- Mostrar cantidad de productos
- Añadir elementos a un producto
'''
# Módulos
import tkinter as tk
from tkinter import ttk, messagebox
class ControlPanel(tk.Tk):
def __init__(self):
super().__init__()
self.title("Panel de control")
self.resizable(False, False)
self.configure(padx=12, pady=12)
# Ejemplo: Inicializamos los valores de la cantidad de los productos de los que disponemos
self.products = {
"Producto 1": 1,
"Producto 2": 10,
"Producto 3": 50,
"Producto 4": 100,
}
self._crear_widgets()
def _crear_widgets(self):
frame = ttk.Frame(self)
frame.grid()
for i, product in enumerate(self.products):
btn = ttk.Button(
frame,
text=product,
command=lambda p=product: self.mostrar_cantidad(p),
width=18
)
btn.grid(row=i // 2, column=i % 2, padx=6, pady=6)
ttk.Separator(frame).grid(row=2, columnspan=2, sticky="ew", pady=8)
# Boton para abrir una ventana nueva y añadir una cantidad de elementos
ttk.Button(
frame,
text="Añadir elementos",
command=self.abrir_anyadir_window
).grid(row=3, columnspan=2, pady=6)
def mostrar_cantidad(self, product):
cant = self.products[product]
messagebox.showinfo(product, f"Disponemos de {cant} elementos")
def abrir_anyadir_window(self):
AnyadirVentana(self)
class AnyadirVentana(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.title("Añadir elementos")
self.resizable(False, False)
self.configure(padx=12, pady=12)
self.product_var = tk.StringVar()
self.cant_var = tk.StringVar()
self._crear_widgets()
def _crear_widgets(self):
ttk.Label(self, text="Producto").grid(row=0, column=0, sticky="w")
product_cb = ttk.Combobox(
self,
textvariable=self.product_var,
values=list(self.parent.products.keys()),
state="readonly",
width=18
)
product_cb.grid(row=1, column=0, pady=4)
product_cb.set("Seleccione un producto")
ttk.Label(self, text="Cantidad a añadir").grid(row=2, column=0, sticky="w", pady=(8, 0))
ttk.Entry(self, textvariable=self.cant_var, width=20).grid(row=3, column=0, pady=4)
# Creación de un botón que llama a la función add_elements
ttk.Button(
self,
text="Añadir",
command=self.add_elements
).grid(row=4, column=0, pady=10)
def add_elements(self):
'''Método para añadir una cantidad de elementos a un producto
'''
product = self.product_var.get()
cant = self.cant_var.get()
if product not in self.parent.products:
messagebox.showwarning(self, "Producto", "Seleccione un producto válido")
return
try:
cant = int(cant)
except ValueError:
messagebox.showerror(self, "Cantidad", "Introduzca un número entero")
return
self.parent.products[product] += cant
self.destroy() # Cerramos la ventana windows
if __name__ == "__main__":
app = ControlPanel()
app.mainloop()