简体   繁体   中英

Tkinter: How can I fix a top-level window not displaying correctly

I'm doing a voting system to try to implement it in my school.

So the program is supposed to go like this:

  1. a function that is not yet implemented will generate some passwords according to the number of students that is going to vote.

  2. The main window displays all the candidates.

  3. When you click on the button, a pop up window (toplevel) appears prompting you to enter your password

  4. If the password is correct, another window pops up (another Toplevel, and here is the problem) showing you some buttons that you just have to click on to choose your candidate.

  5. Repeat until complete

Original Link : http://pastebin.com/579ybmPD

Code

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()

So the problem is that the last toplevel window is appearing on my main window instead of displaying on a new pop up, and one time I got it to work, the options were not displaying properly. Please help.

You misspelled topframe in optionsWindows(). Also you don't pack topf(F)rame so it doesn't appear. Finally you pass a variable to command= incorrectly for the buttons in optionsWindows.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM