我對下面的代碼有問題。現在我對編程一般都很陌生,大部分代碼都是從互聯網上覆制下來的,我對它進行了調整,以便按照我希望的方式工作。所以如果沒有簡單的解決方法,那沒關係。也許你可以指出一些編程或python的主題,我應該閱讀。在另一個函數中使用在一個函數中定義的列表。 Python 2.7
無論如何我試圖解釋它。我已經定義了函數query(),它對sqlite數據庫進行了一些更改。輸入是一個列表。如果我單獨使用它,該功能就可以正常工作。
現在我試圖讓一個接口,我可以定義,應該在該列表內,取決於檢查複選框。然後,當我按下一個按鈕時,我想用該特定列表執行該功能。複選框生成得很好,按鈕也一樣。此外,當我檢查或取消選中按鈕時,它會更新列表,並且它會在解釋器中顯示新的更新列表。
問題是,該按鈕不起作用: 1.它不使用新的更新列表,而是使用空列表() 2.當我輸入一個預定義的列表,這不是空的它會自動運行query()而不用我甚至點擊按鈕。
我可能沒有解釋得很好,但我希望你明白我的問題是什麼。
感謝您的幫助
`
def chkbox_checked():
for ix, item in enumerate(cb):
opt[ix]=(cb_v[ix].get())
print opt
def query(opt):
import sqlite3
connection = sqlite3.connect("gather.sqlite")
cursor1 = connection.cursor()
cursor1.execute('Drop table IF EXISTS matches')
cursor1.execute('CREATE TABLE matches(date TEXT, team1 TEXT, team2 TEXT, league TEXT)')
cursor1.execute('DELETE FROM "main"."matches"')
for i in range(0, len(opt)):
a=opt[i]
cursor1.execute('INSERT INTO matches (date, team1, team2, league) SELECT * FROM gather WHERE team1=? or team2=? or league=?', (a,a,a,))
cursor1.execute('Drop table IF EXISTS matchessorted')
cursor1.execute('CREATE TABLE matchessorted(date TEXT, team1 TEXT, team2 TEXT, league TEXT)')
cursor1.execute('DELETE FROM "main"."matchessorted"')
cursor1.execute('INSERT INTO matchessorted (date, team1, team2, league) SELECT * FROM matches ORDER BY date')
connection.commit()
import Tkinter as tk
from Tkinter import *
opt = []
root = tk.Tk()
mylist = [
'name1',
'name2',
'name3'
]
cb = []
cb_v = []
for ix, text in enumerate(mylist):
cb_v.append(tk.StringVar())
off_value=0
cb.append(tk.Checkbutton(root, text=text, onvalue=text,offvalue=off_value,
variable=cb_v[ix],
command=chkbox_checked))
cb[ix].grid(row=ix, column=0, sticky='w')
opt.append(off_value)
cb[-1].deselect()
label = tk.Label(root, width=20)
label.grid(row=ix+1, column=0, sticky='w')
button1 = Button(root, text = "Calculate", command = query(opt))
button1.grid(column=1, row=0, sticky=W)
root.mainloop()
`
是不是chkbox_checked()填充列表? 所以應該這樣工作? 選項= chkbox_checked 然後 查詢(選項) ? – user2317500
它的工作原理!大!非常感謝。 – user2317500