【问题标题】:How to align these checkbuttons in tkinter?如何在 tkinter 中对齐这些检查按钮?
【发布时间】:2017-11-01 09:04:27
【问题描述】:

我想问一下,如何在 tkinter 中对齐这几个 checkbuttons。我正在使用ttk。

这是我的代码

button_frame=Frame(main, style='TFrame')
checks_frame=Frame(main, style='TFrame')
output_frame=Frame(main, style='TFrame')
start_button=Button(button_frame, text='START', command=lambda: _thread.start_new_thread(suspend_processes, ()), state=NORMAL, style='TButton')
stop_button=Button(button_frame, text='STOP', command=lambda: restore_all_processes(False, False), state=DISABLED, style='TButton')
restore_button=Button(button_frame, text='RESTORE', command=lambda: restore_all_processes(False, True), style='TButton')
scrollbar=Scrollbar(output_frame)
out=Text(output_frame, state=NORMAL, bg='white', width=50, height=10, spacing3=True, wrap=WORD, yscrollcommand=scrollbar.set, font=('Calibri'), foreground=theme)
strict_check=Checkbutton(checks_frame, text='Strict mode (FPS+)', variable=strict, command=switch_strict, style='TCheckbutton')
real_time_check=Checkbutton(checks_frame, text='Use real time priority (experimental, sound may lagg, FPS+)', state=NORMAL, command=switch_real_time, variable=real_time)
dis_game_sound_check=Checkbutton(checks_frame, text='Disable in game sound (recommended, FPS+)', state=DISABLED, command=switch_dis_game_sound, variable=dis_game_sound)
dis_grass_swaying_check=Checkbutton(checks_frame, text='Disable grass swaying (FPS+)', variable=dis_grass_swaying)
dis_per_pixel_point_lighting_check=Checkbutton(checks_frame, text='Disable per pixel point lighting (FPS+)', variable=dis_per_pixel_point_lighting)
slow_client_check=Checkbutton(checks_frame, text='Slow Client (max performance for client)', variable=slow_client)

button_frame                      .pack()
start_button                      .pack(side=LEFT)
stop_button                       .pack(side=LEFT)
restore_button                    .pack(side=LEFT)
checks_frame                      .pack(side=LEFT)
dis_per_pixel_point_lighting_check.pack()
dis_grass_swaying_check           .pack()
slow_client_check                 .pack()
strict_check                      .pack()
real_time_check                   .pack()
dis_game_sound_check              .pack()
output_frame                      .pack(side=RIGHT)
out                               .pack(side=LEFT, fill=BOTH, expand=True)
scrollbar                         .pack(side=LEFT, expand=True, fill=Y)

请帮助我。感谢您的任何回复。

【问题讨论】:

  • 你发布了一堆不相关的代码。你能把它减少到minimal reproducible example吗?另外,您是否阅读过 checkbuttons 和 pack 的文档以查看可用的选项?您提出的问题可以通过阅读可用的文档并进行一些实验来回答。
  • 您是否考虑过使用grid() 而不是pack()
  • 您可以使用 .place(x=#, y=#) 代替,然后您可以准确设置所有内容的位置

标签: python tkinter alignment ttk


【解决方案1】:

我相信您需要在每个检查按钮小部件上设置 pack anchor 属性。默认锚点是“中心”,这就是它们在容器中心对齐的原因。所以这里是修复。

dis_per_pixel_point_lighting_check.pack(side=TOP, anchor=W)
dis_grass_swaying_check           .pack(side=TOP, anchor=W)
slow_client_check                 .pack(side=TOP, anchor=W)
strict_check                      .pack(side=TOP, anchor=W)
real_time_check                   .pack(side=TOP, anchor=W)
dis_game_sound_check              .pack(side=TOP, anchor=W)

【讨论】:

  • 一个例子可以提高这个答案的质量。
  • 我在复选框中有一个多行文本。只有最后一行对齐。第一行仍然居中。任何想法?我在 python 3.6.5
  • 通过设置justify 属性,多行复选框可以使其所有文本左对齐、右对齐或居中(默认居中)。要左对齐多行文本,请尝试:my_checkbutton['justify'] = 'left'
猜你喜欢
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 2012-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
相关资源
最近更新 更多