简体   繁体   English

python tkinter中的字典即使使用grid_forget()也不会在网格中隐藏按钮

[英]dictionary in python tkinter not hiding button in grid even with grid_forget()

EDIT 2: I realized that I forgot to change a variable name at the top to the dictionary... I am very mad now. 编辑2:我意识到我忘了在字典顶部更改变量名了……我现在很生气。

EDIT: I changed all these exec's to a dictionary. 编辑:我将所有这些执行程序更改为词典。

First of all, please don't rage at me for using exec()/eval(). 首先,请不要因为使用exec()/ eval()而生气。 It was a last resort, and I'm not taking input for this. 这是不得已的办法,我没有为此提供意见。 brute force writing out variable names seems too lengthy, and I can't think of a better way to do this. 蛮力写出变量名似乎太冗长,我想不出一种更好的方法来做到这一点。

Anyways, I am writing a calendar application in python tkinter, and changing the month has become a problem for me. 无论如何,我正在用python tkinter编写日历应用程序,而更改月份对我来说已成为一个问题。 In a calendar, whenever someone changes a month, the days obviously begin on a different day of the week (May 2014 begins on a Thursday, and June 2014 on a Sunday). 在日历中,每当有人更改一个月时,这些日子显然会在一周的另一天开始(2014年5月在星期四开始,2014年6月在星期日开始)。

In order to display the days of the month, though, I gave each day a button and its own special variable name using 为了显示月份中的日期,我给每一天都提供了一个按钮及其自己的特殊变量名,使用

exec('self.daynum'+str(7*(w-2)+d)+
     ' = Button(text=day, width=2)')

where w and d were variables for the week number and day number. 其中w和d是星期数和天数的变量。 So, when I want to hide these buttons, I believe I should use 所以,当我想隐藏这些按钮时,我相信我应该使用

exec('self.daynum'+str(day)+'.grid_forget()')

where day is looped over 0-35 and clears the whole grid(in theory). 一天在0-35之间循环并清除整个网格(理论上)。 But, instead it raises an AttributeError, insisting that these variables do not exist. 但是,相反,它引发了AttributeError,坚持认为这些变量不存在。

Is there a way to fix this and hide the buttons I don't want, or suggestions on how to remodel the program to not use these (but still hide buttons I don't want)? 有没有办法解决此问题并隐藏我不想要的按钮,或者关于如何重塑程序以不使用这些按钮的建议(但仍然隐藏我不想要的按钮)?

There is no good way to fix this fundamentally flawed approach. 没有修复这种根本有缺陷的方法的好方法。 There is a much simpler approach, which is to use a dictionary to hold your widgets: 有一种简单得多的方法,即使用字典保存小部件:

widgets = {}
for daynum in range(31):
    widgets[daynum] = Button(text=str(day), width = 2)
...
widgets[daynum].grid_forget()

Doing this will make your code much easier to manage. 这样做将使您的代码更易于管理。

Using lists and dicts, you really don't ever need to use eval/exec. 使用列表和字典,您实际上根本不需要使用eval / exec。 The following example creates a calendar of a month and keeps a list of buttons. 下面的示例创建一个月的日历并保留一个按钮列表。 You can have access to these buttons later using eg dayButtons[0]. 您可以稍后使用例如dayButtons [0]访问这些按钮。 So to hide a button later, you can do dayButtons[day].grid_forget(). 因此,要稍后隐藏按钮,可以执行dayButtons [day] .grid_forget()。

import Tkinter as tk
import datetime

f = tk.Frame()

dayButtons = []

for day in range(1, 32):
    date = datetime.datetime(day=day, month=5, year=2014)
    _, weeknum, weekday = date.isocalendar()

    dayButton = tk.Button(None, text=str(day), width=2)
    dayButton.grid(row=weeknum, column=weekday)
    dayButtons.append(dayButton)

tk.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM