【问题标题】:How do I place tkinter buttons in a specific area?如何将 tkinter 按钮放置在特定区域?
【发布时间】:2019-11-28 21:23:49
【问题描述】:

我正在制作一个程序,用于轻松查找预订联系人。当我输入所有这些信息时,按钮会停留在窗口的左上角。

我怎样才能让它们出现在屏幕中间?更好的是,我一般如何将它们放置在我想要的任何地方? (不用担心网络浏览器的导入,因为它会在程序的后面进行。)

import webbrowser
from tkinter import *
from tkinter import ttk

root = Tk()
root.title('GUI Booking')
root.geometry('600x400')
root.resizable(width=False, height=False)

style = ttk.Style()
style.configure("TButton", 
                font="TkDefaultFont", 
                height=20, 
                width=20,
                padding=10)
main_frame = Frame()
main_frame.grid(row=0, columnspan=4)

# Starting Window
button_location = ttk.Button(main_frame, text='Location').grid(row=1, column=3)
button_name = ttk.Button(main_frame, text='Name').grid(row=2, column=3)
button_email = ttk.Button(main_frame, text='Email').grid(row=3, column=3)




root.mainloop()

【问题讨论】:

  • 这个问题太笼统了。共有三个几何管理器 - 包、放置和网格,每个都可以用来将小部件放在您想要的任何地方。他们都有不同的优点和缺点,每个人在某些事情上都比其他人好,比其他人差。放置小部件的方式高度依赖于其他小部件,以及当有多余空间或空间不足时您期望发生的情况

标签: python-3.x tkinter


【解决方案1】:

使用.place 选项而不是.grid:

...
Button = Button(something, text='Something', command=Something)
Button.place(x=value(e.g:2), y=value(e.g:2))
...

【讨论】:

    【解决方案2】:

    您可以使用行/列配置使按钮居中:http://www.effbot.org/tkinterbook/grid.htm 我已经在你的根配置下添加了这个。最终,小部件的位置将取决于您使用的小部件并使用网格管理器调整它们的位置。

    import webbrowser
    from tkinter import *
    from tkinter import ttk
    
    root = Tk()
    root.title('GUI Booking')
    root.geometry('600x400')
    root.resizable(width=False, height=False)
    
    root.grid_rowconfigure(0, weight=1)  # Added to center buttons
    root.grid_columnconfigure(0, weight=1)  # Added to  center buttons
    
    style = ttk.Style()
    style.configure("TButton",
                font="TkDefaultFont",
                height=20,
                width=20,
                padding=10)
    main_frame = Frame()
    
    
    # Starting Window
    button_location = ttk.Button(main_frame, text='Location').grid(row=1, column=1)
    button_name = ttk.Button(main_frame, text='Name').grid(row=2, column=1)
    button_email = ttk.Button(main_frame, text='Email').grid(row=3, column=1)
    
    main_frame.grid(row=0, column=0)
    
    
    root.mainloop()
    

    【讨论】:

      【解决方案3】:

      如果您希望它们出现在屏幕中间,请使用 .pack 而不是 .grid。

      import webbrowser
      from tkinter import *
      from tkinter import ttk
      root = Tk()
      root.title('GUI Booking')
      root.geometry('600x400')
      root.resizable(width=False, height=False)
      
      style = ttk.Style()
      style.configure("TButton", 
                      font="TkDefaultFont", 
                      height=20, 
                      width=20,
                      padding=10)
      main_frame=Frame()
      
      # Starting Window
      button_location = ttk.Button(main_frame, text='Location').grid()
      button_name = ttk.Button(main_frame, text='Name').grid()
      button_email = ttk.Button(main_frame, text='Email').grid()
      main_frame.pack()
      root.mainloop()
      

      警告:不要在同一窗口中使用.pack 和.grid。

      【讨论】:

        猜你喜欢
        • 2016-07-19
        • 1970-01-01
        • 2021-01-05
        • 2015-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-24
        相关资源
        最近更新 更多