从数据库中随机生成

时间:2015-02-17 12:47:17

标签: python sqlite tkinter

所以我在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

=============================================== =========================

1 个答案:

答案 0 :(得分:1)

由于代码中的for循环,col被指定为您的最后一项。 我的意思是,

lst = ["a","b","c","d"]
for x in lst: 
    pass
print (x)
>>> d

另外,由于您的col只是一个项目,shuffle不会做任何事情。

您可以创建一个新列表,然后将所有项目添加到该列表并随机播放。

new_list = [] #here is your new list. also you can create it by list comp.
count = 0
for col in c :
   print (col)
   new_list.append(col) 
   count = count + 1
print (count, 'Columns.')
c.close()

def DrawList():
        random.shuffle(new_list) 
        button['bg'] = 'blue'
        button['fg'] = 'white'

        for item in new_list:
                listbox.insert(0,item)