【问题标题】:Randomly generate from a database从数据库中随机生成
【发布时间】: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

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

【问题讨论】:

    标签: python sqlite tkinter


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2015-01-21
      相关资源
      最近更新 更多