数据输入错误

时间:2016-08-25 17:24:34

标签: python tkinter

我想为Tkinter上的管理员创建一个控制系统,一些功能(添加,删除,更新和加载)是控制系统的主要部分,但是当我运行代码时,这些功能不起作用,没有错误信息。但是,我无法弄清问题在哪里。我的代码还没有完成。如果我解决了,那么我将转向另一个步骤。

import tkinter
from tkinter import *


userlist = [
  ['Meyers',  '12356'],
  ['Smith','abcde'],
  ['Jones','123abc34'],
  ['Barnhart','12//348'],
  ['Nelson','1234'],
  ["Prefect",'1345'],
  ["Zigler",'8910'],
  ['Smith','1298']]


def domain():

    def whichSelected () :
        print ("At %s of %d" % (select.curselection(), len(userlist)))
        return int(select.curselection()[0])

    def addEntry():
        userlist.append ([nameVar.get(), passwordVar.get()])
        setSelect()

    def updateEntry():
        userlist[whichSelected()] = [nameVar.get(), passwordVar.get()]
        setSelect()

    def deleteEntry():
        del userlist[whichSelected()]
        setSelect()

    def loadEntry():
        name, password = userlist[whichSelected()]
        nameVar.set(name)
        passwordVar.set(password)

    def makeWindow():
        win=Tk()
        global nameVar, passwordVar, select

        frame1 = Frame(win)
        frame1.pack()

        Label(frame1, text="Name").grid(row=0, column=0, sticky=W)
        nameVar = StringVar()
        name = Entry(frame1, textvariable=nameVar)
        name.grid(row=0, column=1, sticky=W)

        Label(frame1, text="Password").grid(row=1, column=0, sticky=W)
        passwordVar= StringVar()
        password= Entry(frame1, textvariable=passwordVar)
        password.grid(row=1, column=1, sticky=W)

        frame2 = Frame(win)       # Row of buttons
        frame2.pack()
        b1 = Button(frame2,text=" Add  ",command=addEntry)
        b2 = Button(frame2,text="Update",command=updateEntry)
        b3 = Button(frame2,text="Delete",command=deleteEntry)
        b4 = Button(frame2,text=" Load ",command=loadEntry)
        b1.pack(side=LEFT); b2.pack(side=LEFT)
        b3.pack(side=LEFT); b4.pack(side=LEFT)

        frame3 = Frame(win)       # select of names

        frame3.pack()
        scroll = Scrollbar(frame3, orient=VERTICAL)
        select = Listbox(frame3, yscrollcommand=scroll.set, height=6)
        scroll.config (command=select.yview)
        scroll.pack(side=RIGHT, fill=Y)
        select.pack(side=LEFT,  fill=BOTH, expand=1)
        return win

    def setSelect():
        userlist.sort()
        select.delete(0,END)
        for name in userlist:
            select.insert(END,name)

    win=makeWindow()
    setSelect()
    win.mainloop()




page1=Tk()
but1=Button(page1,text="Domain",command=domain).pack()

2 个答案:

答案 0 :(得分:0)

在函数中定义函数并使调试变得非常困难是不好的做法。我将首先使用一个对象来创建这个GUI。对象变量:

  • passwordVar和nameVar,
  • 选择
  • 用户列表

答案 1 :(得分:0)

你的代码出了很多问题。

例如,您不需要两次导入tkinter。您的变量名称大小不会跟随PEP8。您可以从OOP方法中受益。

我建议找一个好的IDE代码来突出你的格式和错误。

查看提供的代码:

import tkinter as tk

user_list = [
  ['Meyers',  '12356'],
  ['Smith','abcde'],
  ['Jones','123abc34'],
  ['Barnhart','12//348'],
  ['Nelson','1234'],
  ["Prefect",'1345'],
  ["Zigler",'8910'],
  ['Smith','1298']]


class Domain(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        self.name_var = tk.StringVar()
        self.password_var = tk.StringVar()
        self.make_window()

    def which_selected(self):
        print("At %s of %d" % (self.select.curselection(), len(user_list)))
        return int(self.select.curselection()[0])

    def add_entry(self):
        user_list.append([self.name_var.get(), self.password_var.get()])
        self.set_select()

    def update_entry(self):
        user_list[self.which_selected()] = [
            self.name_var.get(), self.password_var.get()]
        self.set_select()

    def delete_entry(self):
        del user_list[self.which_selected()]
        self.set_select()

    def load_entry(self):
        name, password = user_list[self.which_selected()]
        self.name_var.set(name)
        self.password_var.set(password)

    def make_window(self):
        frame1 = tk.Frame(self.parent)
        frame1.pack()

        tk.Label(frame1, text="Name").grid(row=0, column=0, sticky=tk.W)

        name = tk.Entry(frame1, textvariable=self.name_var)
        name.grid(row=0, column=1, sticky=tk.W)

        tk.Label(frame1, text="Password").grid(row=1, column=0, sticky=tk.W)

        password = tk.Entry(frame1, textvariable=self.password_var)
        password.grid(row=1, column=1, sticky=tk.W)

        frame2 = tk.Frame(self.parent)  # Row of buttons
        frame2.pack()
        b1 = tk.Button(frame2, text=" Add  ", command=self.add_entry)
        b2 = tk.Button(frame2, text="Update", command=self.update_entry)
        b3 = tk.Button(frame2, text="Delete", command=self.delete_entry)
        b4 = tk.Button(frame2, text=" Load ", command=self.load_entry)
        b1.pack(side=tk.LEFT)
        b2.pack(side=tk.LEFT)
        b3.pack(side=tk.LEFT)
        b4.pack(side=tk.LEFT)

        frame3 = tk.Frame(self.parent)  # select of names

        frame3.pack()
        scroll = tk.Scrollbar(frame3, orient=tk.VERTICAL)
        self.select = tk.Listbox(frame3, yscrollcommand=scroll.set, height=6)
        scroll.config(command=self.select.yview)
        scroll.pack(side=tk.RIGHT, fill=tk.Y)
        self.select.pack(side=tk.LEFT,  fill=tk.BOTH, expand=1)

    def set_select(self):
        user_list.sort()
        self.select.delete(0, tk.END)
        for name in user_list:
            self.select.insert(tk.END, name)

if __name__ == '__main__':
    root = tk.Tk()
    Domain(root)
    root.mainloop()

注意:的 这里仍然存在错误,但我并不完全知道您尝试做什么,所以我只是在这里进行了重组,以便您可以开始更好的路径。