python - 组合框不显示在 tkinter 中

标签 python user-interface tkinter

大家好, 我正在尝试创建组合框来输入一些日期,它工作了一段时间,但是,我不知道为什么,它停止出现在我的框架中,我无法修复它。 我还想知道更多的事情:是否可以用 0(如 01、02 等)输入当天的输入,并且还可以使用月份中的某一天的范围之类的东西,而不是全部写入。我不能这样做,因为当我尝试时总是会出错。 感谢您的帮助!

import tkinter as tk
from tkinter import *
import tkinter.ttk

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

###BIRTHDAY
        birthday_label = tk.Label(self, text="Birthday:", font=('times', 14), anchor='e')
        birthday_label.grid(row=3, sticky='e')

        month_day = {
            'January': ['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'],
            'February': ['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'],
            'March': ['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'],
            'April': ['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'],
            'May': ['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'],
            'June': ['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'],
            'July': ['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'],
            'August': ['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'],
            'September': ['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'],
            'October': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '11', '12', '13', '14', '15', '16', '17', '18',
                        '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'],
            'November': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '11', '12', '13', '14', '15', '16', '17', '18',
                         '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'],
            'December': ['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']}

    def getUpdateData(self, event):
        self.day['values'] = month_day[self.month.get()]

        self.day = IntVar(self)
        self.day.set(1)
        self.day = ttk.Combobox(self)
        self.day.grid(row=3, column=1)

        self.month = IntVar(self)
        self.month.set("January")
        self.month = ttk.Combobox(self, values=list(month_day.keys()))
        self.month.bind('<<ComboboxSelected>>', self.getUpdateData)
        self.month.grid(row=3, column=1, sticky='w')

        self.year = IntVar(self)
        self.year.set(2000)
        self.year = ttk.Combobox(self, values=list(range(1940, 2006)))
        self.year.grid(row=3, column=1, sticky='e')


if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

最佳答案

好的,我们开始吧。这些是您的脚本有问题的地方:

  1. 您正在创建 IntVars,然后基本上说“没关系,改为组合框” *组合框接受 StringVar,但您不需要它们
  2. 您正在更新方法中创建所有小部件,尽管这很奇怪,但您从未费心调用更新方法,因此它会创建其中任何一个。
  3. 许多内容都以过于冗长的方式编写
  4. 您应该调用 grid_rowconfiguregrid_columnconfigure 来完成网格配置。
  5. 唯一需要更新的是选择新月份时可以选择的天数。是否实现闰年取决于您。

我完全重写了你的脚本并改变了一切。我的版本的显示方式可能不完全是您想要的,但现在一切正常,您可以很容易地自定义该部分。

# quick and dirty
from tkinter.ttk import *
from tkinter import *

class Application(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self,  *args, **kwargs)
        
        # ranges are your friend
        d29 = list(range(1,30))
        d30 = list(range(1,31))
        d31 = list(range(1,32))
        
        #
        self.months = dict(
            January     = d31,
            February    = d29,
            March       = d31,
            April       = d30,
            May         = d31,
            June        = d30,
            July        = d31,
            August      = d31,
            September   = d30,
            October     = d31,
            November    = d30,
            December    = d31)

        self.lbl_birth = Label(self, text="Birthday:", font=('times', 14))
        self.lbl_birth.grid(row=1, column=1)
        
        self.cb_day = Combobox(self, values=self.months["January"])
        self.cb_day.grid(row=1, column=2)
        self.cb_day.set("1")
        
        self.cb_month = Combobox(self, values=[*self.months])
        self.cb_month.bind('<<ComboboxSelected>>', self.update)
        self.cb_month.grid(row=1, column=3)
        self.cb_month.set("January")
        
        self.cb_year = Combobox(self, values=list(range(1996, 2006)))
        self.cb_year.grid(row=1, column=4)
        self.cb_year.set("2000")
        
        # configure grid
        # this app does not need this configuration BUT you eventually will
        # so why not get used to it right now
        items = [self.lbl_birth, self.cb_day, self.cb_month, self.cb_year]
        for t in items:
            self.grid_columnconfigure(t, weight=1)
            self.grid_rowconfigure(t, weight=1)
        
    def update(self, event):
        # set day values to the appropriate range
        self.cb_day["values"] = self.months[self.cb_month.get()]
        self.cb_day.set("1")


if __name__ == "__main__":
    app = Application()
    app.title('My Birthday App')
    app.mainloop()

关于python - 组合框不显示在 tkinter 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62615283/

相关文章:

python - 行继续字符 python 后的语法错误意外字符

python - 为什么 Flask 中的应用程序上下文不是应用程序的单例?

c# - 如何使用现有的 Windows 功能从 UI 中提取文本

python - Tkinter .after 方法卡住窗口?

python - 在 Python 中无需重启应用程序即可检索 MySQL 数据库的新行

python - 如何使 Python 脚本独立可执行文件在没有任何依赖的情况下运行?

c# - 继承 WPF 窗口

android - 为 Android 应用绘制棋盘(跳棋)

python - 如何在另一个函数中关闭 tkinter 窗口?

python - 如何在 Pandas 数据框中制作相同数量的观察值?