加载黑框时出现 ttk checkbutton
ttk checkbutton appears when loaded up with black box in it
取自:
提供的解决方案适用于一个复选按钮
import tkinter as Tk
from tkinter import IntVar
from tkinter.ttk import Frame, Checkbutton
class TestGui(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.var1 = IntVar()
self.var1.set(1)
button = Checkbutton(parent,
text="Pick me, pick me!",
variable=self.var1) # note difference here
button.grid()
root = Tk.Tk()
app = TestGui(root)
root.mainloop()
但是当我在 class 中使用循环创建多个按钮时,这仅适用于最后一个按钮。
无法真正解释为什么,但我想通了。
不起作用(使用列表):
varss = []
for n in range (2):
date = datetime.today() + timedelta(days=n)
day = datetime.strftime(date, "%d.%m.%y")
for h in range(24):
date = date.replace(hour=h,minute=0)
txt_date = datetime.strftime(date,"%H:%M")
var = IntVar()
c = ttk.Checkbutton(tour_frame,text = txt_date,variable = var)
var.set(1)
c.grid(column = n , row = h + 1)
varss.append(var)
有效(使用字典列表):
l= []
for n in range (2):
date = datetime.today() + timedelta(days=n)
day = datetime.strftime(date, "%d.%m.%y")
for h in range(24):
date = date.replace(hour=h,minute=0)
txt_date = datetime.strftime(date,"%H:%M")
var = IntVar()
c = ttk.Checkbutton(tour_frame,text = txt_date,variable = var)
var.set(1)
c.grid(column = n , row = h + 1)
l.append(dict(datetime = date.replace(hour = h,minute=0,second=False,microsecond=False),value = var ))
取自:
提供的解决方案适用于一个复选按钮
import tkinter as Tk
from tkinter import IntVar
from tkinter.ttk import Frame, Checkbutton
class TestGui(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.var1 = IntVar()
self.var1.set(1)
button = Checkbutton(parent,
text="Pick me, pick me!",
variable=self.var1) # note difference here
button.grid()
root = Tk.Tk()
app = TestGui(root)
root.mainloop()
但是当我在 class 中使用循环创建多个按钮时,这仅适用于最后一个按钮。
无法真正解释为什么,但我想通了。
不起作用(使用列表):
varss = []
for n in range (2):
date = datetime.today() + timedelta(days=n)
day = datetime.strftime(date, "%d.%m.%y")
for h in range(24):
date = date.replace(hour=h,minute=0)
txt_date = datetime.strftime(date,"%H:%M")
var = IntVar()
c = ttk.Checkbutton(tour_frame,text = txt_date,variable = var)
var.set(1)
c.grid(column = n , row = h + 1)
varss.append(var)
有效(使用字典列表):
l= []
for n in range (2):
date = datetime.today() + timedelta(days=n)
day = datetime.strftime(date, "%d.%m.%y")
for h in range(24):
date = date.replace(hour=h,minute=0)
txt_date = datetime.strftime(date,"%H:%M")
var = IntVar()
c = ttk.Checkbutton(tour_frame,text = txt_date,variable = var)
var.set(1)
c.grid(column = n , row = h + 1)
l.append(dict(datetime = date.replace(hour = h,minute=0,second=False,microsecond=False),value = var ))