从数据库中随机生成

Randomly generate from a database

所以我在 sqlite 中创建了一个数据库并在 python 中使用了 tkinter。我已经设法制作了一个 window 允许用户按下将从该列表中随机选择的按钮,但是该列表不是随机生成的并且仅从数据库中选择列表的最终项目。到目前为止,这是我的代码。有什么帮助吗?

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

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

由于代码中的 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)