【问题标题】:how to make my button align in pack method如何使我的按钮在打包方法中对齐
【发布时间】:2021-10-28 02:06:37
【问题描述】:
dates = [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]

month = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUNE', 'JULY', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']

year = [2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038,
        2039, 2040]

Date = StringVar(plane)
Date.set(dates[0])

Month = StringVar(plane)
Month.set(month[0])

Year = StringVar(plane)
Year.set(year[0])

date_option = OptionMenu(plane, Date, *dates)
month_option = OptionMenu(plane, Month, *month)
year_option = OptionMenu(plane, Year, *year)

date_option.config(font=("Arial", 18, "bold"))
month_option.config(font=("Arial", 18, "bold"))
year_option.config(font=("Arial", 18, "bold"))

date_option.pack(side="left", fill="x", expand="yes")
month_option.pack(side="left", fill="x", expand="yes")
year_option.pack(side="left", fill="x", expand="yes")

submit = Button(plane, text="Submit", font=("Engrave", 15, "bold"), fg="black", bg="silver")
submit.pack(side="right", padx=30)

如果我将提交按钮放在年份选项菜单旁边,我无法将提交按钮打包在日期输入下拉框下方。

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    OptionMenu 放在一个框架中更容易:

    ...
    frame = Frame(plane)
    frame.pack()
    
    date_option = OptionMenu(frame, Date, *dates)
    month_option = OptionMenu(frame, Month, *month)
    year_option = OptionMenu(frame, Year, *year)
    ...
    

    【讨论】:

      【解决方案2】:

      你需要声明一个新的Frame来放置按钮:

      from tkinter import *
      
      r = Tk()
      
      r2 = Frame(r)
      r2.pack(side = 'bottom')
      
      Label(r, text = 'right') .pack(fill = 'x', side = 'right')
      Label(r2, text = 'left') .pack(side = 'left')
      
      r.mainloop()
      

      使用 .pack() 更快,但在创建 GUIS 时灵活性要小得多,我建议将 .grid() 用于实验以外的任何事情。

      .pack() resources

      【讨论】:

        猜你喜欢
        • 2016-05-11
        • 2021-12-19
        • 2010-11-05
        • 2021-07-15
        • 1970-01-01
        • 2019-02-04
        • 2020-07-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多