【发布时间】:2015-02-17 12:47:16
【问题描述】:
所以我在 sqlite 中创建了一个数据库,并在 python 中使用了 tkinter。我设法制作了一个窗口,允许用户按下按钮,该按钮将从该列表中随机选择,但是该列表不是随机生成的,并且仅从数据库中选择列表的最后一项。到目前为止,这是我的代码..有什么帮助吗?
from tkinter import
import random
import sqlite3
conn = sqlite3.connect('cashflow.db')#creates database file
c = conn.cursor()
def tableCreate(): #creates the multiplechoice table
c.execute("CREATE TABLE multiplechoice(Equation VARCHAR, Answer VARCHAR)")
conn.commit()
def dataEntry(): #Enters the equations in the table
c.execute("INSERT INTO multiplechoice VALUES('average selling price x goods sold','sales revenue')")
c.execute("INSERT INTO multiplechoice VALUES('price x number of customers','revenue')")
c.execute("INSERT INTO multiplechoice VALUES('total revenue of company / total industry revenue x 100','market share percantage')")
c.execute("INSERT INTO multiplechoice VALUES('fixed cost / selling price - variable costs','break even')")
c.execute("INSERT INTO multiplechoice VALUES('sales income- variable costs','total contibution')")
c.execute("INSERT INTO multiplechoice VALUES('sales income - break even output','margin of safety')")
c.execute("INSERT INTO multiplechoice VALUES('change in market share / original market size x 100','market growth')")
c.execute("INSERT INTO multiplechoice VALUES('net profit / revenue x 100','net profit margin')")
c.execute("INSERT INTO multiplechoice VALUES('net profit/capital employed x 100','Retrn of capital employed')")
#c.execute("UPDATE multiplechoice")
conn.commit()
c.execute('SELECT Equation FROM multiplechoice')
count = 0
for col in c :
print (col)
count = count + 1
print (count, 'Columns.')
c.close()
import random
def DrawList():
random.shuffle(col)
plist = col
button['bg'] = 'blue'
button['fg'] = 'white'
for item in plist:
listbox.insert(0,item);
root = Tk() #This creates a window, but it won't show up
root.title("Multiple choice")
root.geometry("450x250+100+100")
labeltext = StringVar()
labeltext.set(" Question one is: " )
label3= Label(root, textvariable = labeltext,)
listbox = Listbox(root)
button = Button(root,text = "Randomise",command = DrawList)
button.pack()
listbox.pack() #this tells the listbox to come out
root.mainloop() #This command will tell the window come out
================================================ ==========================
【问题讨论】: