Created
May 26, 2025 17:46
-
-
Save ihopeudie/bb031b8848ebb3b20f8a4eada3bcd60b to your computer and use it in GitHub Desktop.
form and creation screen of 2nd module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
import tkinter.ttk as ttk | |
import mysql.connector | |
def db_config(): | |
return { | |
'host': 'localhost', | |
'user': 'root', | |
'password': '1234', | |
'database': 'Test1' | |
} | |
def get_product_list(): | |
query = """ | |
SELECT pt.title AS product_type, | |
p.title AS product_name, | |
p.sku AS sku, | |
p.minimal_price AS minimal_price, | |
mt.title AS material_type, | |
ROUND(SUM(pw.production_time), 2) AS production_time | |
FROM product p | |
LEFT JOIN product_type pt ON p.product_type_id = pt.id | |
LEFT JOIN material_type mt ON p.material_type_id = mt.id | |
LEFT JOIN product_workshop pw ON p.id = pw.product_id | |
GROUP BY pt.title, p.title, p.sku, p.minimal_price, mt.title | |
""" | |
connection = mysql.connector.connect(**db_config()) | |
cursor = connection.cursor() | |
cursor.execute(query) | |
rows = cursor.fetchall() | |
cursor.close() | |
connection.close() | |
return rows | |
def get_product_types(): | |
query = """ | |
select title, id from product_type | |
""" | |
return get_dict(query) | |
def get_dict(query): | |
connection = mysql.connector.connect(**db_config()) | |
cursor = connection.cursor() | |
cursor.execute(query) | |
rows = cursor.fetchall() | |
cursor.close() | |
connection.close() | |
return {row[0]: row[1] for row in rows} | |
def clean_widgets(root): | |
for widget in root.winfo_children(): | |
widget.destroy() | |
def go_back(root): | |
clean_widgets(root) | |
draw_main_screen(root) | |
def save_to_db(form): | |
product_type_id = { | |
"Гостиные": 1, | |
"Мягкая мебель":2 | |
}.get(form.get('product_type'), -1) | |
material_type_id = { | |
"Лист": 1, | |
"Металл":2 | |
}.get(form.get('material'), -1) | |
print(product_type_id, material_type_id) | |
query = """ | |
insert into product (title, sku, minimal_price, product_type_id, material_type_id) values (%s, %s, %s, %s, %s) | |
""" | |
connection = mysql.connector.connect(**db_config()) | |
cursor = connection.cursor() | |
cursor.execute(query, [ | |
form['product_name'], | |
form['sku'], | |
form['minimal_price'], | |
product_type_id, | |
material_type_id | |
]) | |
connection.commit() | |
cursor.close() | |
connection.close() | |
print("Продукт успешно сохранён") | |
def create_item(root, form): | |
print(form) | |
save_to_db(form) | |
clean_widgets(root) | |
draw_main_screen(root) | |
def draw_create_form(root): | |
# артикул, тип продукта (выпадающий список), наименование, минимальная стоимость для партнера, основной материал. | |
frame = tk.Frame(root) | |
frame.grid(row=0, column=0, sticky=tk.NSEW) | |
root.title("Создать продукт") | |
header_label = tk.Label(frame, text='Создание продукта', font=('Candara', 18)) | |
header_label.grid(column=0, row=0, sticky=tk.W) | |
sku_label = tk.Label(frame, text="Артикул:") | |
sku_label.grid(column=0, row=1, sticky=tk.E, padx=5, pady=5) | |
sku_input = tk.Entry(frame) | |
sku_input.grid(column=1, row=1, sticky=tk.W, padx=5, pady=5) | |
product_type_label = tk.Label(frame, text="Тип продукта:") | |
product_type_label.grid(column=0, row=2, sticky=tk.E, padx=5, pady=5) | |
product_type_input = ttk.Combobox(frame, values=list(["Гостиные", "Мягкая мебель", "что еще"])) | |
product_type_input.current(0) | |
product_type_input.grid(column=1, row=2, sticky=tk.W, padx=5, pady=5) | |
name_label = tk.Label(frame, text="Наименование:") | |
name_label.grid(column=0, row=3, sticky=tk.E, padx=5, pady=5) | |
name_input = tk.Entry(frame) | |
name_input.grid(column=1, row=3, sticky=tk.W, padx=5, pady=5) | |
min_price_label = tk.Label(frame, text="Мин цена:") | |
min_price_label.grid(column=0, row=4, sticky=tk.E, padx=5, pady=5) | |
min_price_input = tk.Entry(frame) | |
min_price_input.grid(column=1, row=4, sticky=tk.W, padx=5, pady=5) | |
material_label = tk.Label(frame, text="Основной материал") | |
material_label.grid(column=0, row=5, sticky=tk.E, padx=5, pady=5) | |
material_input = ttk.Combobox(frame, values=list(["Лист", "Металл", "что еще"])) | |
material_input.current(0) | |
material_input.grid(column=1, row=5, sticky=tk.W, padx=5, pady=5) | |
back_button = tk.Button(frame, text="Назад", command=lambda:go_back(root)) | |
back_button.grid(column=1, row=6, sticky=tk.W, padx=5, pady=5) | |
create_button = tk.Button(frame, text="Создать", command=lambda:create_item(root, { | |
"sku": sku_input.get(), | |
"product_type": product_type_input.get(), | |
"product_name": name_input.get(), | |
"minimal_price": min_price_input.get(), | |
"material": material_input.get() | |
})) | |
create_button.grid(column=2, row=6, sticky=tk.W, padx=5, pady=5) | |
def move_to_create_screen(root): | |
clean_widgets(root) | |
draw_create_form(root) | |
def draw_buttons(root): | |
create_button = tk.Button(root, text="Создать", command=lambda:move_to_create_screen(root)) | |
create_button.grid(row=1, column=0, padx=10, pady=10, sticky=tk.W) | |
def draw_main_screen(root): | |
draw_table(root) | |
draw_buttons(root) | |
def draw_table(root): | |
table = ttk.Treeview(root, columns=('1', '2', '3', '4', '5', '6'), show='headings', height=10) | |
products = get_product_list() | |
# ('Гостиные', 'Комплект мебели для гостиной Ольха горная', '1549922', 160507.0, 'Мебельный щит из массива дерева', 7.0) | |
table.heading('1', text='Тип продукции') | |
table.heading('2', text='Название') | |
table.heading('3', text='Артикул') | |
table.heading('4', text='Минимальная цена поставщика') | |
table.heading('5', text='Основной материал') | |
table.heading('6', text='Время изготовления') | |
for product in products: | |
table.insert('', 'end', values=(product[0], product[1], product[2], product[3], product[4], product[5])) | |
table.grid(row=0, column=0, padx=10, pady=10, sticky=tk.NSEW) | |
def main(): | |
root = tk.Tk() | |
root.geometry('1200x600') | |
root.grid_rowconfigure(0, weight=1) | |
root.grid_columnconfigure(0, weight=1) | |
draw_main_screen(root) | |
root.mainloop() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment