是素素呀 发表于 2019-2-21 10:40:29

from tkinter import *
master = Tk()

GIRLS = ["貂蝉","西施","王昭君","杨玉环"]

v = []

for girl in GIRLS:
    v.append(InVar())
    b = Checkbutton(master,text=girl,variable=v[-1])
    b.pack()
   
mainloop()

为什么我运行这个程序会报“NameError: name 'InVar' is not defined”这个错呢?

liugan 发表于 2019-6-3 23:13:54

钱越来越少了

阳顺 发表于 2019-7-11 14:52:03

.pack(anchor=w)

最后报错NameError: name 'w' is not defined


c = Checkbutton(root, text="加特技", variable=v).pack()
#c.var= v   #新建变量?
from tkinter import *
root = Tk()

Girls = ["xi shi","diao can","wang zhaojun","yang yuhuang"]
v = []

for i in Girls:
    v.append(IntVar())
    b = Checkbutton(root,text=i,variable=v[-1]).pack()
    #b.pack(side=LEFT)一行并排
    #b.pack(anchor=w)?报错


v = IntVar()

c = Checkbutton(root, text="加特技", variable=v).pack()
c.var= v   #新建变量?



mainloop()

AttributeError: 'NoneType' object has no attribute 'var'为哈?

nichengang 发表于 2019-8-10 22:58:07

学了3个tk组件了,基本都是一样的,就像小甲鱼说的那样,认真学会一个就一通百通了

15295030992 发表于 2020-4-12 19:05:37

{:10_256:}

最好是明天 发表于 2020-8-30 14:58:13

import tkinter as tk
from tkinter import ttk

app=tk.Tk()

v=tk.IntVar()
ttk.Checkbutton(app,font=('宋体',20,'bold'),text='我是复选',variable=v).pack()

app.mainloop()

错误:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\checkbuttonfont.py", line 7, in <module>
    ttk.Checkbutton(app,font=('宋体',20,'bold'),text='我是复选',variable=v).pack()
File "c:\program files\python37\lib\tkinter\ttk.py", line 637, in __init__
    Widget.__init__(self, master, "ttk::checkbutton", kw)
File "c:\program files\python37\lib\tkinter\ttk.py", line 559, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "c:\program files\python37\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-font"

这个该怎么解决啊!!!

王小也 发表于 2022-9-6 08:02:42

创建复选框,同时获得选中的选项
names_list = ["wwx", "wxh", "yx", "hlh", "fzl", "lyb"]

v = []

for name in names_list:
    v.append(tk.IntVar())
    cb = tk.Checkbutton(master, text=name, variable=v[-1])
    cb.pack(anchor="w")

def showResult():
    print("选中的内容是:")
    for i in range(len(v)):
      if v.get() == 1:# 判断该选项是否被选中
            print(names_list)


b = tk.Button(master, text="确定", command=showResult)
b.pack()
页: 1 [2]
查看完整版本: Tkinter 窗口组件:Checkbutton