gpt4 book ai didi

python - 组合框不显示在 tkinter

转载 作者:行者123 更新时间:2023-12-04 09:35:31 24 4
gpt4 key购买 nike

今天是个好日子,
我正在尝试创建组合框来输入一些日期,它工作了一段时间,但我不知道为什么,它停止出现在我的框架中,我无法修复它。
我还想知道更多的事情:是否可以使用 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()

最佳答案

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

  • 您正在创建 IntVars 然后基本上说“没关系成为一个组合框”
    *组合框接受 StringVar 但你不需要它们
  • 您在 update 方法中创建所有小部件,即使这很奇怪,您也从不费心调用 update 方法,因此它会创建任何一个。
  • 很多东西都写得太冗长了
  • 您应该调用 grid_rowconfiguregrid_columnconfigure完成网格配置。
  • 唯一需要更新的是选择新月份时可以选择的天数。实现闰年取决于您。

  • 我完全重写了你的剧本并改变了一切。我的版本的显示方式可能并不完全符合您的要求,但现在一切正常,您可以轻松自定义该部分。
    # 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com