我正在使用python3.x创建一个多语言的“日历”小部件,它有单独的年、月、日等的数字框。一个要求是无论使用什么小部件,都不能在任何时候隐藏GUI的任何部分,从而排除了下拉列表等,这使得spinbox成为一个明显的选择。在
如果有人知道在哪里可以找到纯python/tkinter“date&time entry”widgit(理想情况下可以处理闰年和所有月末情况)的公共域源代码,请向我指出该存储库。在
我能找到的最接近的解决方案是这个OptionMenu解决方案(Change OptionMenu based on what is selected in another OptionMenu),但由于“不要隐藏”的要求,它无法使用。以此作为灵感(在查看了http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/spinbox.html上的Spinbox文档之后),我想出了下面的代码。为了简单起见,我删除了处理闰年和月末问题的代码。除以下两个问题外,代码正常工作:
问题1–我无法让“DaySpinBox”动态调整其“范围”以将自己设置为“MonthSpinBox”中的月份:
根据我阅读Mark Lutz的“编程Python”一书(第三版,第383和384页),我应该能够动态地设置DaySpinBox的最大/最后一天(通过使用“values=”或“to=”和“from斨”选项)来匹配MonthSpinBox中出现的月份。我在跟踪MonthSpinBox变量(“SelectedMonth”),根据书中的描述,我尝试使用以下方法更新DaySpinBox:
1-使用“to=SelectedMonth”。一些尝试在创建DaySpinBox的调用中显示为注释。(我刚接触lambdas,可能我没有正确使用它们?),
2-使用“DaySpinBox['to']=SelectedMonth”,和
3-使用“DaySpinBox.config文件(收件人=选定月份)“。
我也尝试过在所有方法中使用“values=”,但都没有成功(首选“to=…..”选项)。在
问题1:是否可以将数字调整框设置为使用动态范围,或者,正如我开始怀疑的那样,它们的范围在创建了数字调整框之后是不变的吗?在
问题2:经过大量的网络搜索,我还没有找到任何明确说明哪些Spinbox参数(“to=”、“from=”和“value=”关键字)可以动态更改,哪些参数不能更改–在哪里可以找到这些信息(针对所有小部件类型)?在
问题2–MonthSpinBox总是初始化为一月而不是当前月份
我使用“textvariable”关键字将年、月和日微调框初始化为“today”。这适用于YearSpinBox和DaySpinBox,但不适用于MonthSpinBox。(令人恼火的是,我认为MonthSpinBox可以正常工作,但我在修复DaySpinBox时弄坏了它)。唯一明显的区别是年和日数字轴使用整数,而月数字轴盒使用字符串。有什么建议吗?在
对于这两个问题,我考虑了LEGB问题的可能性,但是没有函数被嵌套,所以变量隐藏不应该是一个问题-除非我的一个变量是重复的并且隐藏了一个在tkinter中定义的变量,等等
我错过了什么?在
顺便说一句,我知道使用全局变量的利弊,也知道PEP8(http://www.python.org/dev/peps/pep-0008/)-请参阅目录中的第二个要点。;>;)
谢谢。在
from tkinter import *
from tkinter import ttk
from datetime import datetime
from collections import OrderedDict
root = Tk() # put here so IntVar (etc.) is useable during initialization
StartUpDateTime = datetime.now()
DefaultYear = StartUpDateTime.year
DefaultMonthNumber = StartUpDateTime.month
DefaultDayOfMonth = StartUpDateTime.day
# These are only used to build the Month name & length dictionary
YearAndMonthLengths = [ 365, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
EnglishMonthNames = ( 'Entire Year', 'January', 'February', 'March',
'April', 'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December' )
FrenchMonthNames = ( 'année entière', 'janvier', 'février', 'mars',
'avril', 'mai', 'juin', 'juillet', 'août', 'septembre',
'octobre', 'novembre', 'décembre' )
#SpanishMonthNames = ....
#ItalianMonthNames = ....
Month_Names = FrenchMonthNames
Month_Names = EnglishMonthNames # comment out this line to use French
# Create dictionary containing month names that appear in the Month spinbox.
# OrderedDict used because Month name (=key) field must be language-independent.
DaysInMonth = OrderedDict()
for i in range( 0, len( Month_Names ) ) :
DaysInMonth[ Month_Names[ i ] ] = YearAndMonthLengths[ i ]
DefaultMonthName = Month_Names[ DefaultMonthNumber ]
DefaultMonthLength = DaysInMonth[ DefaultMonthName ]
# Initialize the Spinbox interface variables to todays date
SelectedYear = IntVar( value = DefaultYear )
SelectedMonthName = StringVar( value = DefaultMonthName )
SelectedMonthLength = IntVar( value = DefaultMonthLength )
SelectedDay = IntVar( value = DefaultDayOfMonth )
#-------------------------------------------------------------------------------
def GetChosenMonthLength( *args ) :
global SelectedMonthLength
SelectedMonthLength.set( DaysInMonth[ SelectedMonthName.get() ] )
return
#-------------------------------------------------------------------------------
mainframe = Frame( root )
mainframe.grid( row = 0, column = 0 )
# Show today's date. If it's July 17th, 2023 the spinboxes must be initialized
# to "July" ("julliet" if using French), "17" and "2023".
YearSpinBox = Spinbox( mainframe,
from_ = 2000, to = 2050,
textvariable = SelectedYear,
wrap = TRUE, state = 'readonly', width = 5 )
MonthSpinBox = Spinbox( mainframe,
values = tuple( DaysInMonth.keys() )[1:],
textvariable = SelectedMonthName,
wrap = TRUE, state = 'readonly', width = 10 )
DaySpinBox = Spinbox( mainframe,
from_ = 1,
to = SelectedMonthLength.get(),
# to = DaysInMonth[ SelectedMonthName.get() ],
# values = tuple( str( i ) for i in range( 1, SelectedMonthLength.get() + 1 ) ),
# values = lambda : tuple( str( i ) for i in range( 1, DaysInMonth[ SelectedMonthName.get() ] + 1 ) )
textvariable = SelectedDay,
wrap = TRUE, state = 'readonly', width = 16
)
MonthSpinBox.grid( row = 0, column = 0 )
DaySpinBox.grid( row = 0, column = 1 )
YearSpinBox.grid( row = 0, column = 2 )
SelectedMonthName.trace( 'w', GetChosenMonthLength )
mainloop()
顺便说一句,示例代码省略了证明所有Spinbox接口变量都被正确更新的调试代码,包括包含已更改/新月份最后一天的变量(使用“to=”关键字“发送”到日期微调框)。在
所有控件的所有选项都可以动态配置。如果我没记错的话,只有一个例外,这是几乎没有人使用过的特性:框架上的
class
选项。在关于更新day数字调整框,我看不出你想在哪里更新它,所以我不知道你为什么认为它应该更新。要更新数字调整框,请将命令附加到月份数字调整框,然后在回调中进行更新。在
例如:
至于为什么月份不设为本月。。。我不知道。看来你做得对。这个小部件与其他小部件的唯一区别是这是一个StringVar而不是一个IntVar,并且month数字调整框有一个
values
参数。也许这是tkinter中的一个bug或文档记录不好的特性,是由这种差异引发的。在一个简单的解决方法是在创建month spinbox之后添加
SelectedMonthName.set(DefaultMonthName)
,以将stringvar重置为正确的默认值。在相关问题 更多 >
编程相关推荐