tkinter-如何获取变量中的输入数据

时间:2018-07-31 15:49:41

标签: python python-3.x tkinter tkinter-entry

我正在为本地供应商制作工人管理应用程序。我声明的textvariables不能正常工作。而不是获取输入的idNumber,我总是在控制台上获取0。(在savedata函数中) 如何从我的文本变量获取数据?

from tkinter import *
root = Tk()

在此处声明变量

idNumber = IntVar()
Name = StringVar()
FName = StringVar()
Address = StringVar()
Mobile = StringVar()
IDCard = StringVar()
DOJ = StringVar()
Skill = StringVar()
Wage = StringVar()
OT = StringVar()

主Gui窗口

class mainWindow:
    def __init__(self):
        pass

    def MainWindow(self, mainW):
        mainW.config(background='black')
        Label(mainW, text='Welcome to Worker Management App...', font=('Comic Sans MS', 32, 'bold'), fg='white',
              bg='black').grid(row=1, column=1)
        Label(mainW, text='Choose a category:', font=('Comic Sans MS', 32, 'bold'), fg='white', bg='black').grid(row=2,
                                                                                                                 column=1)
        Label(mainW, bg='black').grid(row=3, column=1)
        Label(mainW, bg='black').grid(row=4, column=1)
        Button(mainW, text='1. Individual', font=('Comic Sans MS', 28), fg='white', bg='black', command=guiIndi).grid(
            row=5, column=1)
        Label(mainW, bg='black').grid(row=6, column=1)
        Label(mainW, bg='black').grid(row=7, column=1)
        b2 = Button(mainW, text='2. Contractor', font=('Comic Sans MS', 28), fg='white', bg='black')
        b2.grid(row=8, column=1)
        Label(mainW, bg='black').grid(row=9, column=1)


def guiIndi():
        B1 = Button(root, text='Register New', font=('Comic Sans MS', 20), fg='white', bg='black')
        B1.place(x=100, y=285)

        indi = Individual()

        B1.config(command=indi.IndiGui)
        B2 = Button(root, text='Existing', font=('Comic Sans MS', 20), fg='white', bg='black')
        B2.place(x=580, y=285)
        Label(root, bg='black', fg='white').grid(row=7)
        Label(root, bg='black', fg='white').grid(row=8)
        Label(root, bg='black', fg='white').grid(row=9)
        Label(root, bg='black', fg='white').grid(row=10)

我正在尝试访问数据的功能

def savedata(idNumber, Name, FName, Address, Mobile, IDCard,DOJ, Skill, Wage, OT):

    print(idNumber.get())

要在其中输入数据的类:

class Individual():
    def __init__(self):
        pass

    def IndiGui(self):
        b = Tk()
        b.config(background='black')
        # cursor.execute('''
        # CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT,fname TEXT,
        #                 address TEXT, mobile TEXT,idcard TEXT ,
        #             doj TEXT, skill TEXT, wage TEXT, ot TEXT)
        # ''')
        # db.commit()

        Label(b, fg='white', bg='black').grid(row=0)
        l1 = Label(b, text='ID Number: ', fg='white', bg='black')
        l1.grid(row=1, column=0, sticky=W, padx=15)
        e1 = Entry(b, textvariable=idNumber)
        e1.grid(row=1, column=1, sticky=W, padx=15)
        Label(b, fg='white', bg='black').grid(row=2)
        l2 = Label(b, text='Name: ', fg='white', bg='black')
        l2.grid(row=3, column=0, sticky=W, padx=15)
        e2 = Entry(b, textvariable=Name)
        e2.grid(row=3, column=1, sticky=W, padx=15)
        Label(b, fg='white', bg='black').grid(row=4)
        l3 = Label(b, text="Father's Name:  ", fg='white', bg='black')
        l3.grid(row=5, column=0, sticky=W, padx=15)
        e3 = Entry(b, textvariable=FName)
        e3.grid(row=5, column=1, sticky=W, padx=15)
        Label(b, fg='white', bg='black').grid(row=6)
        l4 = Label(b, text='Address: ', fg='white', bg='black')
        l4.grid(row=7, column=0, sticky=W, padx=15)
        e4 = Entry(b, textvariable=Address)
        e4.grid(row=7, column=1, sticky=W, padx=15)
        Label(b, fg='white', bg='black').grid(row=8)
        l5 = Label(b, text='Mobie Number ', fg='white', bg='black')
        l5.grid(row=9, column=0, sticky=W, padx=15)
        e5 = Entry(b, textvariable=Mobile)
        e5.grid(row=9, column=1, sticky=W, padx=15)
        Label(b, fg='white', bg='black').grid(row=10)
        l6 = Label(b, text='ID Card Number: ', fg='white', bg='black')
        l6.grid(row=11, column=0, sticky=W, padx=15)
        e6 = Entry(b, textvariable=IDCard)
        e6.grid(row=11, column=1, sticky=W, padx=15)
        Label(b, fg='white', bg='black').grid(row=12)
        l7 = Label(b, text='Date of Joining: ', fg='white', bg='black')
        l7.grid(row=13, column=0, sticky=W, padx=15)
        e7 = Entry(b, textvariable=DOJ)
        e7.grid(row=13, column=1, sticky=W, padx=15)
        Label(b, fg='white', bg='black').grid(row=14)
        l8 = Label(b, text='Skill: ', fg='white', bg='black')
        l8.grid(row=15, column=0, sticky=W, padx=15)
        e8 = Entry(b, textvariable=Skill)
        e8.grid(row=15, column=1, sticky=W, padx=15)
        Label(b, fg='white', bg='black').grid(row=16)
        l9 = Label(b, text='Wages: ', fg='white', bg='black')
        l9.grid(row=17, column=0, sticky=W, padx=15)
        e9 = Entry(b, textvariable=Wage)
        e9.grid(row=17, column=1, sticky=W, padx=15)
        Label(b, fg='white', bg='black').grid(row=18)
        l9 = Label(b, text='Over Time: ', fg='white', bg='black')
        l9.grid(row=19, column=0, sticky=W, padx=15)
        e9 = Entry(b, textvariable=OT)
        e9.grid(row=19, column=1, sticky=W, padx=15)
        Label(b, fg='white', bg='black').grid(row=20)
        b1 = Button(b, text='Go', fg='white', bg='black', font=(14))
        b1.grid(row=21, column=0, columnspan=2)
        b1.config(command=lambda: 
       savedata(idNumber,Name,FName,Address,Mobile,IDCard,DOJ,Skill,Wage,OT))

        Label(b, fg='white', bg='black').grid(row=22)
        b.mainloop()




app = mainWindow()
app.MainWindow(root)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

快速解决方案是将b = Tk()类中的b = Toplevel(root)更改为Individual():。除此之外,您还需要重新编写一些代码,以使DRY更干燥。

我觉得看到此代码以DRY编写并遵循PEP8标准,您会从中受益。

在这里,您的代码被重写以更好地遵循PEP8和DRY惯例。更少的空格,下面的代码只有42行代码。

import tkinter as tk


lbl_list = ['ID Number: ', 'Name: ', "Father's Name:  ", 'Address: ',
            'Mobie Number ', 'ID Card Number: ', 'Date of Joining: ',
            'Skill: ', 'Wages: ', 'Over Time: ', 'Go']

string_var_list = []
entry_list = []

font32 = ('Comic Sans MS', 32, 'bold')
font28 = ('Comic Sans MS', 28, 'bold')
font20 = ('Comic Sans MS', 20)


class MainWindow(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.config(background='black')
        tk.Label(self, text='Welcome to Worker Management App...', font=font32, fg='white', bg='black').grid(row=1, column=0, columnspan=3)
        tk.Label(self, text='Choose a category:', font=font32, fg='white', bg='black').grid(row=2, column=0, columnspan=3)
        tk.Button(self, text='1. Individual', font=font28, fg='white', bg='black', command=self.gui_indi).grid(row=3, column=1, pady=25)
        b2 = tk.Button(self, text='2. Contractor', font=font28, fg='white', bg='black')
        b2.grid(row=4, column=1, pady=25)

    def gui_indi(self):
        B1 = tk.Button(self, text='Register New', font=font20, fg='white', bg='black')
        B1.grid(row=2, column=0, rowspan=3, sticky="e")
        B1.config(command=lambda: Individual(self))
        B2 = tk.Button(self, text='Existing', font=font20, fg='white', bg='black')
        B2.grid(row=2, column=2, rowspan=3, sticky="w")

    def savedata(self):
        for strvar in string_var_list:
            print(strvar.get())


class Individual(tk.Toplevel):
    def __init__(self, master):
        tk.Toplevel.__init__(self)
        self.config(background='black')

        for i in range(len(lbl_list)):
            string_var_list.append(tk.StringVar())
            entry_list.append(tk.Entry(self, textvariable=string_var_list[i]))

        for ndex, lbl_name in enumerate(lbl_list):
            if lbl_name != "Go":
                tk.Label(self, text=lbl_name, fg='white', bg='black').grid(row=ndex+1, column=0, sticky="w", padx=15)
                entry_list[ndex].grid(row=ndex+1, column=1, sticky="w", padx=15, pady=10)
            else:
                tk.Button(self, text=lbl_name, fg='white', bg='black', font=(14), command=self.master.savedata).grid(row=21, column=0, columnspan=2, pady=10)


app = MainWindow()
app.mainloop()