如何将输入从条目添加到python中的标签?

时间:2018-11-18 22:46:35

标签: python tkinter sqlite

我正在使用pythons tkinter和sqlite3制作注册表。该程序运行良好,但是我不知道如何从条目中获取内容并使用该输入创建标签。这是完整的代码:

from tkinter import *
import sqlite3

root = Tk()
root.geometry('500x500')
root.title("Registration Form")

Fullname = StringVar()
Email = StringVar()
var = IntVar()
c = StringVar()
var1 = IntVar()


def database():
    name1 = Fullname.get()
    email = Email.get()
    gender = var.get()
    country = c.get()
    prog = var1.get()
    conn = sqlite3.connect('tennis.db')
    with conn:
        cursor = conn.cursor()
    cursor.execute(
        'CREATE TABLE IF NOT EXISTS Student (Fullname TEXT,Email TEXT,Gender TEXT,country TEXT,Programming TEXT)')
    cursor.execute('INSERT INTO Student (FullName,Email,Gender,country,Programming) VALUES(?,?,?,?,?)',
               (name1, email, gender, country, prog,))
    conn.commit()


label_0 = Label(root, text="Registration form", width=20, font=("bold", 20))
label_0.place(x=90, y=53)

label_1 = Label(root, text="FullName", width=20, font=("bold", 10))
label_1.place(x=80, y=130)

entry_1 = Entry(root, textvar=Fullname)
entry_1.place(x=240, y=130)

label_2 = Label(root, text="Email", width=20, font=("bold", 10))
label_2.place(x=68, y=180)

entry_2 = Entry(root, textvar=Email)
entry_2.place(x=240, y=180)

label_3 = Label(root, text="Gender", width=20, font=("bold", 10))
label_3.place(x=70, y=230)

Radiobutton(root, text="Male", padx=5, variable=var, value=1).place(x=220, y=230)
Radiobutton(root, text="Female", padx=20, variable=var, value=2).place(x=290, y=230)

label_4 = Label(root, text="City", width=20, font=("bold", 10))
label_4.place(x=70, y=280)

list1 = ['Africa', 'Nigeria', 'Iceland', 'Other'];

droplist = OptionMenu(root, c, *list1)
droplist.config(width=15)
c.set('select your city')
droplist.place(x=240, y=280)

label_4 = Label(root, text="Skill Level", width=20, font=("bold", 10))
label_4.place(x=85, y=330)
var2 = IntVar()
var3 = IntVar()

Checkbutton(root, text="Beginner", variable=var1).place(x=200, y=330)

Checkbutton(root, text="Intermiadiate", variable=var2).place(x=280, y=330)

Checkbutton(root, text="Advanced", variable=var3).place(x=390, y=330)

def printdatshoot():
    newtk = Tk()
    newtk.geometry('500x500')
    conn = sqlite3.connect('tennis.db')
    with conn:
        cursor = conn.cursor()
    cursor.execute("SELECT * FROM Student")
    result = cursor.fetchone()
    conn.commit()
    for r in result:
        Label(newtk, text=r)

johy = Button(root, text='Submit', width=20, bg='brown', fg='white', command=database)
johy.place(x=180, y=380)
johy1 = Button(root, text='See all', width=20, bg='brown', fg='white', command=printdatshoot)
johy1.place(x=180, y=400)

root.mainloop()

这是我需要帮助的程序的一部分

def printdatshoot():
        newtk = Tk()
        newtk.geometry('500x500')
        conn = sqlite3.connect('tennis.db')
        with conn:
            cursor = conn.cursor()
        cursor.execute("SELECT * FROM Student")
        result = cursor.fetchone()
        conn.commit()
        for r in result:
            Label(newtk, text=r)

我想知道如何将sql查询输出放入标签

1 个答案:

答案 0 :(得分:1)

您忘记为新创建的标签使用几何图形管理器(packgridplace中的一个)。

此外,Tkinter应用程序中的实例不得超过一个Tk()实例。尝试使用Toplevel()创建新窗口。