python tkinter radiobutton用按钮更改

时间:2019-03-28 05:29:59

标签: python tkinter radio-button

如何在tkinter中使用按钮功能更改单选按钮?  然后更改单选按钮的文本值。这是一个问答游戏。问题库存储在sqlite3中,按“下一步”按钮,单选按钮获得新答案,标签获得新问题。

import sqlite3
import tkinter as tk
import tkinter.messagebox
window=tk.Tk()
window.title('Python Qusz game')
window.geometry('400x400')
v=tk.StringVar()
v.set(1)
score=0
num=0
conn = sqlite3.connect('test.db')
c=conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS COMPANY 
       (QUESTION      TEXT     NOT NULL,
       ANSWER_A       TEXT     NOT NULL,
       ANSWER_B       TEXT     NOT NULL,
       ANSWER_C       TEXT,
       ANSWER_D       TEXT,
       RIGHT_ANSWER   TEXT);''')
c = conn.cursor()

c.execute("INSERT INTO COMPANY (QUESTION,ANSWER_A,ANSWER_B,ANSWER_C,ANSWER_D,RIGHT_ANSWER) \
      VALUES ('qus_1','ans_1','ans_2','ans_3','ans_4','right_ans1')")
c.execute("INSERT INTO COMPANY (QUESTION,ANSWER_A,ANSWER_B,ANSWER_C,ANSWER_D,RIGHT_ANSWER) \
      VALUES ('qus_2','ans_1','ans_2','ans_3','ans_4','right_ans2')")
c.execute("INSERT INTO COMPANY (QUESTION,ANSWER_A,ANSWER_B,ANSWER_C,ANSWER_D,RIGHT_ANSWER) \
      VALUES ('qus_3','ans_1','ans_2','ans_3','ans_4','right_ans3')")
c.execute("INSERT INTO COMPANY (QUESTION,ANSWER_A,ANSWER_B,ANSWER_C,ANSWER_D,RIGHT_ANSWER) \
      VALUES ('qus_4','ans_1','ans_2','ans_3','ans_4','right_ans4')")


cursor=c.execute("select QUESTION, ANSWER_A, ANSWER_B, ANSWER_C, ANSWER_D, RIGHT_ANSWER from COMPANY")
value=cursor.fetchall()

var=tk.StringVar()
var.set(value[0][0])
l1=tk.Label(window,textvariable=var)
l1.pack()

def next_and_judge():
   global score
   global num
   r1 = tk.Radiobutton(window, text=value[num][num+1], variable=v, value=1)
   r1.pack()
   r2 = tk.Radiobutton(window, text=value[num][num+2], variable=v, value=2)
   r2.pack()
   r3 = tk.Radiobutton(window, text=value[num][num+3], variable=v, value=3)
   r3.pack()
   r4 = tk.Radiobutton(window, text=value[num][num+4], variable=v, value=4)
   r4.pack()
   num=num+1
   if var.get()==value[num-1][5]:
        score=10+score

b1=tk.Button(window,text='next',command=next_and_judge)
b1.pack()

def sum():
    tkinter.messagebox.showinfo(title='END',message=str(score))

b2=tk.Button(window,text='finish')
b2.pack()


conn.commit()
print('operation done successfully')
conn.close()
window.mainloop()

使用sqlite3

1 个答案:

答案 0 :(得分:0)

您不必更改每次点击next时都会创建单选按钮的情况,只需更改这些按钮的text的值即可显示新答案。

这是一种方法。此代码将在按下start时创建带有第一个问题答案的单选按钮,并在按下next时不断更新问题和答案:

var=tk.StringVar()
var.set('Click start')
l1=tk.Label(window,textvariable=var)
l1.pack()
radio_buttons = []

def show_first(radio_buttons, b1):
    global num, radio_buttons, var
    for idx in range(1, 5):
        radio_buttons.append(tk.Radiobutton(window, text=value[num][num+idx], variable=v, value=idx))
        radio_buttons[-1].pack()
    var.set(value[0][0])
    b1.config(text='next', command=lambda: next_and_judge(radio_buttons))

def next_and_judge(radio_buttons):
    global num, score, value, var
    if var.get()==value[num][5]:
        score+=10
    num=num+1
    for idx in range(1, 5):
        radio_buttons[idx-1].config(text=value[num][num+idx])
    var.set(value[num][num])

b1=tk.Button(window,text='start')
b1.config(command=lambda: show_first(radio_buttons,b1))
b1.pack()