1

我正在做一个投票系统,试图在我的学校实施它。

所以程序应该是这样的:

  1. 尚未实现的功能将根据要投票的学生人数生成一些密码。

  2. 主窗口显示所有候选人。

  3. 单击按钮时,会出现一个弹出窗口(顶层),提示您输入密码

  4. 如果密码正确,则会弹出另一个窗口(另一个 Toplevel,这就是问题所在)向您显示一些按钮,您只需单击这些按钮即可选择您的候选人。

  5. 重复直到完成

原文链接: http: //pastebin.com/579ybmPD

代码

from Tkinter import *
import tkMessageBox
class app(object):
    def __init__(self, parent):
        self.availableCodes = [1,2, 3, 4, 5]
        top = self.top = Toplevel(parent)
        self.label1 = Label(top, text = "Ingrese su contrasena")
        self.label1.pack()
        self.entry1 = Entry(top)
        self.entry1.pack()
        self.button1 = Button(top, text = "Ingrese", command = self.ok)
        self.button1.pack(pady= 5)
        self.button1val = 0
        self.button2val = 0
        self.button3val = 0
        self.button4val = 0
        self.button5val = 0
        self.button6val = 0
        self.button7val = 0
        self.button8val = 0
        self.button9val = 0
        self.button10val = 0
        self.button11val = 0
        self.button12val = 0
    def ok(self):
        self.code = int(self.entry1.get())
        self.voteWindow(self.code, self.availableCodes)
        self.top.destroy()
    def voteWindow(self, code, listOfCodes):
        if code in listOfCodes:
            print "True"
            self.optionsWindows(self.top)
            listOfCodes.remove(code)
        else:
            print "False"
    def optionsWindows(self, parent):
        new = self.new = Toplevel(parent)
        self.topframe = Frame(new)
        self.button1 = Button(self.topFrame, text = "Proyecto 1", command = self.close(self.button1val))#( self.button1val))
        self.button1.pack(side = LEFT)
        self.button2 = Button(self.topFrame, text = "Proyecto 2", command =  self.close(self.button2val))#(options, self.button2val))
        self.button2.pack(side = LEFT)
        self.button3 = Button(self.topFrame, text = "Proyecto 3", command =  self.close(self.button3val))#(options, self.button3val))
        self.button3.pack(side = LEFT)
        self.button4 = Button(self.topFrame, text = "Proyecto 4", command =  self.close(self.button4val))#(options, self.button4val))
        self.button4.pack(side = LEFT)
        self.button5 = Button(self.topFrame, text = "Proyecto 5", command =  self.close(self.button5val))#(options, self.button5val))
        self.button5.pack(side = LEFT)
        self.button6 = Button(self.topFrame, text = "Proyecto 6", command =  self.close(self.button6val))#(options, self.button6val))
        self.button6.pack(side = LEFT)
        self.button7 = Button(self.topFrame, text = "Proyecto 7", command = self.close(self.button7val))#(options, self.button7val))
        self.button7.pack(side = LEFT)
        self.button8 = Button(self.topFrame, text = "Proyecto 8", command =  self.close(self.button8val))#(options, self.button8val))
        self.button8.pack(side = LEFT)
        self.button9 = Button(self.topFrame, text = "Proyecto 9", command =  self.close(self.button9val))#(options, self.button9val))
        self.button9.pack(side = LEFT)
        self.button10 = Button(self.topFrame, text = "Proyecto 10", command =  self.close(self.button10val))#(options, self.button10val))
        self.button10.pack(side = LEFT)
        self.button11 = Button(self.topFrame, text = "Proyecto 11", command =  self.close(self.button11val))#(options, self.button11val))
        self.button11.pack(side = LEFT)
        self.button12 = Button(self.topFrame, text = "Proyecto 12", command = self.close(self.button12val))#(options, self.button12val))
        self.button12.pack(side = LEFT)

    def close(self, variable):

        variable +=1

        self.top.destroy()


def onClick():

    run = app(root)

    root.wait_window(run.top)


root = Tk()
root.configure(bg = "white")
mainButton = Button(root, text='Click aqui para votar', command=onClick)
mainButton.pack()
root.mainloop()

所以问题是最后一个顶级窗口出现在我的主窗口上,而不是显示在新的弹出窗口上,有一次我让它工作,选项显示不正确。请帮忙。

4

1 回答 1

0

您在 optionsWindows() 中拼错了 topframe。你也没有打包 topf(F)rame 所以它不会出现。最后,对于 optionsWindows 中的按钮,您将一个变量错误地传递给 command=。

from Tkinter import *
import tkMessageBox
from functools import partial

class app(object):
        def __init__(self, parent):
                self.availableCodes = [1,2, 3, 4, 5]
                top = self.top = Toplevel(parent)
                self.label1 = Label(top, text = "Ingrese su contrasena")
                self.label1.pack()
                self.entry1 = Entry(top)
                self.entry1.pack()
                self.button1 = Button(top, text = "Ingrese", command = self.ok)
                self.button1.pack(pady= 5)

        def ok(self):
                self.code = int(self.entry1.get())
                self.voteWindow()
                self.top.destroy()

        def voteWindow(self):
                print "vote window"
                if self.code in self.availableCodes:
                        print "True"
                        self.optionsWindows(self.top)
                        self.availableCodes.remove(code)
                else:
                        print "False"

        def optionsWindows(self, parent):
                new = self.new = Toplevel(parent)
                self.topFrame = Frame(new)
                self.topFrame.pack()
                for num in range(12):
                    button = Button(self.topFrame, text = "Proyecto %s" % (num+1),
                             command = partial(self.close, num))
                    button.pack(side="left")

        def close(self, variable):
                variable +=1
                print variable, "passed to self.close()"
                self.top.destroy()


def onClick():

        run = app(root)

        root.wait_window(run.top)


root = Tk()
root.configure(bg = "white")
mainButton = Button(root, text='Click aqui para votar', command=onClick)
mainButton.pack()
Button(root, text="Exit", command=root.quit).pack(side="bottom")
root.mainloop()
于 2015-05-26T01:35:32.477 回答