将用户输入从Tkinter小部件传输到SQLite数据库

时间:2016-11-12 01:49:57

标签: python sqlite tkinter

我是一个尝试使用Tkinter和SQLite的新Python用户(v3.5)。我有一个问题是从小部件(组合和旋转框)获取用户输入到变量,然后将这些变量提供给SQLite数据库。我自己试图诊断问题,我相信问题是将小部件中的数据转换为变量。我已经修改了代码并在网上搜索了解决方案,但我还没有能够获得任何工作。相关代码如下。为了简洁起见,我试图破解不相关的部分。

from tkinter import *
from tkinter.ttk import *
import sqlite3

conn = sqlite3.connect("database.db")
c = conn.cursor()


class GUI:
    def __init__(self):
        self.root = Tk()
        self.player_selection_and_score()
        self.process_button()
        self.create_table()

    def player_selection_and_score(self):
        player = StringVar
        top = LabelFrame(self.root)
        top.grid(column=0, row=0)


        player1_selection = Combobox(top, width=10, textvariable=player, state='readonly')
        player1_selection["values"] = ("Player1", "Player2", "Player3")
        player1_selection.grid(column=1, row=0, sticky="w")
        player1_selection.current(0)
        player1_selection.bind("<<ComboboxSelected>>")
        global player1_var
        player1_var = player1_selection.get()

        player1_score_entry = Spinbox(top, width=5, from_=0, to=10)
        player1_score_entry.grid(column=4, row=0)
        player1_score_entry.bind("<<SpinboxSelected>>")
        global player1_score_var
        player1_score_var = player1_score_entry.get()

def process_button(self):
    bottom = LabelFrame(self.root)
    bottom.grid(column=0, row=2)

    process_button = Button(bottom, text="Process Result", command=self.data_entry)
    process_button.pack()

def create_table(self):
    c.execute("CREATE TABLE IF NOT EXISTS fixtures (player1 TEXT, player1_score REAL)")

def data_entry(self):
    c.execute("INSERT INTO fixtures (player1, player1_score) VALUES (?, ?, ?, ?)", (player1_var, player1_score_var))
    conn.commit()

player1_var = GUI()
player1_score_var = GUI()

# Start GUI
gui = GUI()
gui.root.mainloop()

conn.close()
c.close()

我非常确定问题出在这些特定代码行中:

player1_selection.bind("<<ComboboxSelected>>")
global player1_var
player1_var = player1_selection.get()


player1_score_entry.bind("<<SpinboxSelected>>")
global player1_score_var
player1_score_var = player1_score_entry.get()

谢谢!

1 个答案:

答案 0 :(得分:0)

在您的代码中,您可以在开始时从ComboboxSpinbox获得价值,而不是在他们更改价值时获得价值。

通常,您必须将函数绑定到"<<ComboboxSelected>>"中的command=Spinboxget()将使用bind("<<ComboboxSelected>>", self.on_combobox_select)来获取所选值。

但是你不需要它,因为你按下按钮就能获得价值。

在评论中查看更多内容。

我保留Spinbox(... , command=self.on_spinbox_select)import tkinter as tk import tkinter.ttk as ttk import sqlite3 class GUI: def __init__(self): self.root = tk.Tk() self.player_selection_and_score() self.process_button() self.create_table() def player_selection_and_score(self): #self.player = StringVar() # forgot (), use `self` top = tk.LabelFrame(self.root) top.grid(column=0, row=0) self.player1_selection = ttk.Combobox(top, width=10, state='readonly') # , textvariable=player self.player1_selection["values"] = ("Player1", "Player2", "Player3") self.player1_selection.grid(column=1, row=0, sticky="w") self.player1_selection.current(0) self.player1_selection.bind("<<ComboboxSelected>>", self.on_combobox_select) #global player1_var # better use `self.` #player1_var = player1_selection.get() # useless self.player1_score_entry = tk.Spinbox(top, width=5, from_=0, to=10, command=self.on_spinbox_select) self.player1_score_entry.grid(column=4, row=0) #global player1_score_var # better use `self.` #player1_score_var = player1_score_entry.get() # useless def on_combobox_select(self, event): print("Combobox:", event.widget.get()) def on_spinbox_select(self): print("Spinbox:", self.player1_score_entry.get()) def process_button(self): bottom = tk.LabelFrame(self.root) bottom.grid(column=0, row=2) process_button = tk.Button(bottom, text="Process Result", command=self.data_entry) process_button.pack() def create_table(self): c.execute("CREATE TABLE IF NOT EXISTS fixtures (player1 TEXT, player1_score REAL)") def data_entry(self): player1_var = self.player1_selection.get() player1_score_var = self.player1_score_entry.get() # too many `?` c.execute("INSERT INTO fixtures (player1, player1_score) VALUES (?, ?)", (player1_var, player1_score_var)) conn.commit() #player1_var = GUI() # ??? something stupid #player1_score_var = GUI() # ??? something stupid # --- main --- conn = sqlite3.connect("database.db") c = conn.cursor() gui = GUI() gui.root.mainloop() c.close() # first close `c` later `conn` conn.close() ,但您不需要它。

gem install bundler
bundle install