Anonymous
Виджеты позиционирования Python в сетке
Сообщение
Anonymous » 28 ноя 2024, 22:42
Извините за мой плохой английский, я говорю по-французски.
Я начинаю разработку на Python и пытаюсь использовать Tkinter для программирования моего графического интерфейса, но у меня возникли некоторые проблемы с выравнивание некоторых столбцов в моем приложении Python, которое разделено на 3 столбца.
Код: Выделить всё
root = tk.Tk()
root.title('My first App')
root.option_add("*tearOff", False)
root.columnconfigure(index=0, weight=1)
root.columnconfigure(index=1, weight=1)
root.columnconfigure(index=2, weight=1)
root.rowconfigure(index=0, weight=1)
root.rowconfigure(index=1, weight=1)
root.rowconfigure(index=2, weight=1)
frame = ttk.Frame(root)
frame.pack()
# 1st column
widgets_frame = ttk.LabelFrame(frame, text="Informations")
widgets_frame.grid(row=0, column=0, sticky='nsew', rowspan=3)
# 2nd column
prof_frame = ttk.LabelFrame(frame, text="Situation")
prof_frame.grid(row=0, column=1, sticky='nsew', columnspan=2, rowspan=3)
# 3rd column
studies_frame = ttk.LabelFrame(frame, text="Diplômes")
studies_frame.grid(row=0, column=4, sticky='nsew')
Для 1-го столбца проблем нет.
А вот для 2-го я разделяю его на 2 строки:
1-я строка разделена на 2 столбца
col1: профессиональная ситуация,
col2: может содержать изображение пользователя в первой строке и информация о крови во второй строка.
Вот первая проблема: когда я помещаю строки с информацией о крови, все идет не так.
2-я линии нет pb.
И, наконец, информация моего последнего столбца находится не там, где она может быть, а во втором блоке.
Вот мой код
Код: Выделить всё
**# 2nd column row 1
prof_frame = ttk.LabelFrame(frame, text="Situation")
prof_frame.grid(row=0, column=1, sticky='nsew', columnspan=2, rowspan=3)
statuts_frame = ttk.LabelFrame(prof_frame, text="Statut", padding=(20, 10))
statuts_frame.grid(row=0, column=0, sticky='nsew', columnspan=2, rowspan=6, padx=(20, 10), pady=(20, 10))
check_1 = ttk.Checkbutton(statuts_frame, text="Stagiaire", variable=c)
check_1.state(["alternate"])
check_1.grid(row=1, column=0, padx=5, pady=10, sticky="nsew")
check_2 = ttk.Checkbutton(statuts_frame, text="Intégré", variable=c)
check_2.state(["alternate"])
check_2.grid(row=2, column=0, padx=5, pady=10, sticky="nsew")
check_2 = ttk.Checkbutton(statuts_frame, text="MONP", variable=c)
check_2.state(["alternate"])
check_2.grid(row=3, column=0, padx=5, pady=10, sticky="nsew")
check_3 = ttk.Checkbutton(statuts_frame, text="Retraité", variable=c)
check_3.state(["alternate"])
check_3.grid(row=4, column=0, padx=5, pady=10, sticky="nsew")
check_4 = ttk.Checkbutton(statuts_frame, text="Décédé", variable=c)
check_4.state(["alternate"])
check_4.grid(row=5, column=0, padx=5, pady=10, sticky="nsew")
photo_frame = ttk.LabelFrame(prof_frame, text="Photo", padding=(10, 10))
photo_frame.grid(row=0, column=1, sticky='nsew', columnspan=2, rowspan=3, padx=(20, 10), pady=(20, 10))
photo_image = PhotoImage(file='./assets/icons/upload_image.png')
up_photo_btn = ttk.Button(photo_frame, text='Photo', image=photo_image, compound=CENTER)
up_photo_btn.grid(row=0, column=0)
sang_frame = ttk.LabelFrame(photo_frame, text="Sang", padding=(5,5))
sang_frame.grid(row=1, column=1, sticky='nsew', columnspan=2, rowspan=3)**
# I want to align it verticaly but it look like difficult
radio_1 = ttk.Radiobutton(sang_frame, text="A", variable=d, value=1)
radio_1.grid(row=0, column=0, padx=5, pady=10, sticky="nsew")
radio_2 = ttk.Radiobutton(sang_frame, text="A-B", variable=d, value=2)
radio_2.grid(row=1, column=0, padx=5, pady=10, sticky="nsew")
radio_3 = ttk.Radiobutton(sang_frame, text="B", variable=d, value=1)
radio_3.grid(row=2, column=0, padx=5, pady=10, sticky="nsew")
radio_4 = ttk.Radiobutton(sang_frame, text="O", variable=d, value=2)
radio_4.grid(row=3, column=0, padx=5, pady=10, sticky="nsew")
код последнего столбца
Код: Выделить всё
parcours_frame = ttk.LabelFrame(frame, text="Diplômes")
parcours_frame.grid(row=0, column=4, sticky='nsew')
dip_civiles = ttk.LabelFrame(parcours_frame, text="Statut", padding=(20, 10))
statuts_frame.grid(row=0, column=0, sticky='nsew', columnspan=2, rowspan=6, padx=(20, 10), pady=(20, 10))
check_cep = ttk.Checkbutton(statuts_frame, text="CEP", variable=c)
check_cep.state(["alternate"])
check_cep.grid(row=0, column=0, padx=5, pady=10, sticky="nsew")
check_bepc = ttk.Checkbutton(statuts_frame, text="BEPC", variable=c)
check_bepc.state(["alternate"])
check_bepc.grid(row=0, column=1, padx=5, pady=10, sticky="nsew")
check_bac = ttk.Checkbutton(statuts_frame, text="BAC", variable=c)
check_bac.state(["alternate"])
check_bac.grid(row=0, column=2, padx=5, pady=10, sticky="nsew")
check_lic = ttk.Checkbutton(statuts_frame, text="Licence", variable=c)
check_lic.state(["alternate"])
check_lic.grid(row=1, column=0, padx=5, pady=10, sticky="nsew")
check_mast = ttk.Checkbutton(statuts_frame, text="Master", variable=c)
check_mast.state(["alternate"])
check_mast.grid(row=1, column=1, padx=5, pady=10, sticky="nsew")
check_doc = ttk.Checkbutton(statuts_frame, text="Doctorat", variable=c)
check_doc.state(["alternate"])
check_doc.grid(row=1, column=2, padx=5, pady=10, sticky="nsew")
Мой результат
1- Col2.row1.col1
2- Col2.row1.col2
3- Col3. row0.col0
Подробнее здесь:
https://stackoverflow.com/questions/792 ... -on-a-grid
1732822976
Anonymous
Извините за мой плохой английский, я говорю по-французски. Я начинаю разработку на Python и пытаюсь использовать Tkinter для программирования моего графического интерфейса, но у меня возникли некоторые проблемы с выравнивание некоторых столбцов в моем приложении Python, которое разделено на 3 столбца. [code]root = tk.Tk() root.title('My first App') root.option_add("*tearOff", False) root.columnconfigure(index=0, weight=1) root.columnconfigure(index=1, weight=1) root.columnconfigure(index=2, weight=1) root.rowconfigure(index=0, weight=1) root.rowconfigure(index=1, weight=1) root.rowconfigure(index=2, weight=1) frame = ttk.Frame(root) frame.pack() # 1st column widgets_frame = ttk.LabelFrame(frame, text="Informations") widgets_frame.grid(row=0, column=0, sticky='nsew', rowspan=3) # 2nd column prof_frame = ttk.LabelFrame(frame, text="Situation") prof_frame.grid(row=0, column=1, sticky='nsew', columnspan=2, rowspan=3) # 3rd column studies_frame = ttk.LabelFrame(frame, text="Diplômes") studies_frame.grid(row=0, column=4, sticky='nsew') [/code] Для 1-го столбца проблем нет. А вот для 2-го я разделяю его на 2 строки: 1-я строка разделена на 2 столбца [list] [*]col1: профессиональная ситуация, [*]col2: может содержать изображение пользователя в первой строке и информация о крови во второй строка. [/list] Вот первая проблема: когда я помещаю строки с информацией о крови, все идет не так. 2-я линии нет pb. И, наконец, информация моего последнего столбца находится не там, где она может быть, а во втором блоке. Вот мой код [code]**# 2nd column row 1 prof_frame = ttk.LabelFrame(frame, text="Situation") prof_frame.grid(row=0, column=1, sticky='nsew', columnspan=2, rowspan=3) statuts_frame = ttk.LabelFrame(prof_frame, text="Statut", padding=(20, 10)) statuts_frame.grid(row=0, column=0, sticky='nsew', columnspan=2, rowspan=6, padx=(20, 10), pady=(20, 10)) check_1 = ttk.Checkbutton(statuts_frame, text="Stagiaire", variable=c) check_1.state(["alternate"]) check_1.grid(row=1, column=0, padx=5, pady=10, sticky="nsew") check_2 = ttk.Checkbutton(statuts_frame, text="Intégré", variable=c) check_2.state(["alternate"]) check_2.grid(row=2, column=0, padx=5, pady=10, sticky="nsew") check_2 = ttk.Checkbutton(statuts_frame, text="MONP", variable=c) check_2.state(["alternate"]) check_2.grid(row=3, column=0, padx=5, pady=10, sticky="nsew") check_3 = ttk.Checkbutton(statuts_frame, text="Retraité", variable=c) check_3.state(["alternate"]) check_3.grid(row=4, column=0, padx=5, pady=10, sticky="nsew") check_4 = ttk.Checkbutton(statuts_frame, text="Décédé", variable=c) check_4.state(["alternate"]) check_4.grid(row=5, column=0, padx=5, pady=10, sticky="nsew") photo_frame = ttk.LabelFrame(prof_frame, text="Photo", padding=(10, 10)) photo_frame.grid(row=0, column=1, sticky='nsew', columnspan=2, rowspan=3, padx=(20, 10), pady=(20, 10)) photo_image = PhotoImage(file='./assets/icons/upload_image.png') up_photo_btn = ttk.Button(photo_frame, text='Photo', image=photo_image, compound=CENTER) up_photo_btn.grid(row=0, column=0) sang_frame = ttk.LabelFrame(photo_frame, text="Sang", padding=(5,5)) sang_frame.grid(row=1, column=1, sticky='nsew', columnspan=2, rowspan=3)** # I want to align it verticaly but it look like difficult radio_1 = ttk.Radiobutton(sang_frame, text="A", variable=d, value=1) radio_1.grid(row=0, column=0, padx=5, pady=10, sticky="nsew") radio_2 = ttk.Radiobutton(sang_frame, text="A-B", variable=d, value=2) radio_2.grid(row=1, column=0, padx=5, pady=10, sticky="nsew") radio_3 = ttk.Radiobutton(sang_frame, text="B", variable=d, value=1) radio_3.grid(row=2, column=0, padx=5, pady=10, sticky="nsew") radio_4 = ttk.Radiobutton(sang_frame, text="O", variable=d, value=2) radio_4.grid(row=3, column=0, padx=5, pady=10, sticky="nsew") [/code] код последнего столбца [code]parcours_frame = ttk.LabelFrame(frame, text="Diplômes") parcours_frame.grid(row=0, column=4, sticky='nsew') dip_civiles = ttk.LabelFrame(parcours_frame, text="Statut", padding=(20, 10)) statuts_frame.grid(row=0, column=0, sticky='nsew', columnspan=2, rowspan=6, padx=(20, 10), pady=(20, 10)) check_cep = ttk.Checkbutton(statuts_frame, text="CEP", variable=c) check_cep.state(["alternate"]) check_cep.grid(row=0, column=0, padx=5, pady=10, sticky="nsew") check_bepc = ttk.Checkbutton(statuts_frame, text="BEPC", variable=c) check_bepc.state(["alternate"]) check_bepc.grid(row=0, column=1, padx=5, pady=10, sticky="nsew") check_bac = ttk.Checkbutton(statuts_frame, text="BAC", variable=c) check_bac.state(["alternate"]) check_bac.grid(row=0, column=2, padx=5, pady=10, sticky="nsew") check_lic = ttk.Checkbutton(statuts_frame, text="Licence", variable=c) check_lic.state(["alternate"]) check_lic.grid(row=1, column=0, padx=5, pady=10, sticky="nsew") check_mast = ttk.Checkbutton(statuts_frame, text="Master", variable=c) check_mast.state(["alternate"]) check_mast.grid(row=1, column=1, padx=5, pady=10, sticky="nsew") check_doc = ttk.Checkbutton(statuts_frame, text="Doctorat", variable=c) check_doc.state(["alternate"]) check_doc.grid(row=1, column=2, padx=5, pady=10, sticky="nsew") [/code] Мой результат 1- Col2.row1.col1 2- Col2.row1.col2 3- Col3. row0.col0 Подробнее здесь: [url]https://stackoverflow.com/questions/79235165/python-positioning-widgets-on-a-grid[/url]