我正在制作一个小程序,我有2个复选框(text1和text2)。 我想将值(text1和text2)附加到List,如果选中复选框。 我想打印列表[text1,text2]
from tkinter import *
myApp = Tk()
myApp.title("GUI app")
myApp.geometry("300x500")
List = []
varList = []
var1 = IntVar()
Checkbutton1 = Checkbutton(myApp, text="Text1", variable=var1,
onvalue=1, offvalue=0)
Checkbutton1.grid(row=0, column=1, sticky=W)
var2 = IntVar()
Checkbutton2=Checkbutton(myApp, text="Text2", variable=var2,
onvalue=1, offvalue=0)
Checkbutton2.grid(row=1, column=1,sticky=W)
varList.append(var1)
varList.append(var2)
def addtolist():
for item in varList:
if item.get() == 1:
List.append(item)
print(List)
b1 = Button(myApp, text="Add", command=addtolist)
b1.grid(row=1, column=2)
mainloop()
答案 0 :(得分:2)
使用> payments@1.0.0 start /Users/shawnmurray
> node app.js
module.js:538
throw err;
^
Error: Cannot find module '/Users/shawnmurray/app.js'
at Function.Module._resolveFilename (module.js:536:15)
at Function.Module._load (module.js:466:25)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3 npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! payments@1.0.0 start: `node app.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the payments@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in: npm ERR! /Users/shawnmurray/.npm/_logs/2017-11-26T01_03_27_930Z-debug.log
shawns-iMac:~ shawnmurray$ node -version node: bad option: -version
shawns-iMac:~ shawnmurray$ node --version
v8.9.1
和onvalue="Text", offvalue=""
,然后var1 = StringVar()
将返回item.get()
或空字符串。
"Text"
答案 1 :(得分:1)
from tkinter import *
def addtolist():
global List
List = []
for item in varList:
if item.get() != "":
List.append(item.get())
print(List)
List = []
varList = []
myApp = Tk()
myApp.title("GUI app")
myApp.geometry("300x500")
class Check:
x = 0
def __init__(self, lbl):
self.var = StringVar()
self.cb = Checkbutton(myApp, text=lbl, variable=self.var,
onvalue=lbl, offvalue="")
self.cb.grid(row=Check.x, column=1, sticky=W)
Check.x += 1
varList.append(self.var)
Check("Paul Weller")
Check("Nancy Reagan")
Check("Richard Gere")
b1 = Button(myApp, text="Add", command=addtolist)
b1.grid(row=1, column=2)