新窗口未显示图像/按钮(tkinter)

时间:2017-08-31 01:15:45

标签: python python-3.x tkinter

我正在尝试从主程序打开第二个窗口(由“学习”功能定义) - 窗口打开没有问题,但按钮/图像没有显示。有谁知道我做错了什么?所有图像都是.gif文件,并与程序位于同一文件夹中。

我在一个单独的python文件中的'learn'下编写了代码。最初我曾尝试将该文件作为单独的程序打开,但它不起作用。

from tkinter import *
import linecache
import os

root = Tk()
root.geometry('1200x800') #Dimensions of the window
root.title("stud(y)ious") #title of the window
container1 = Frame(root)
container1.pack(side = TOP, anchor = W)

container2= Frame(root) #Bottom container
container2.pack(side = BOTTOM,pady = 50) #space between the bottom of the timetable and the two buttons; initially this was cropping the timetable too much so this value was DECREASED in order to shorten the amount of padding that exists around the two buttons at the bottom 

#All the images used on the home page; keeping them all in one place so they can be eaisly replaced by anotehr programmer if need be
imgNote = PhotoImage(file='img_note.gif') #'N O T E'
imgHeader = PhotoImage(file='img_title.gif') #'stud(y)ious' 'T I M E T A B L E'
imgTimetable = PhotoImage(file='btn_timetable.gif') #replacable timetable image
imgCreateBtn = PhotoImage(file='btn_create.gif') #'create'
imgLearnBtn = PhotoImage(file='btn_learn.gif') #'learn'

toolbar = Canvas()
Header = Label(toolbar, image = imgHeader).pack() #creation of header, space for imgHeader; above and seperated from main buttons
toolbar.pack()

def destroy(): #closing the window
    root.destroy()

def create(): #The window that appears when the 'create' button is clicked
    toplevel = Toplevel()
    labelCreateHeader = Label(toplevel, image=imgNote) #header graphic
    labelCreateHeader.pack()
    labelCreateText = Label(toplevel, text=txtCreate) #text (from variable noted below)
    labelCreateText.pack()

def learn():  #The window that appears when the 'learn' button is clicked
    window1 = Tk()
    window1.geometry('1200x800')
    window1.title("stud(y)ious: Learn") #title of the window, colon to show connection to studyious_main.py

        #Opens the questions file (in read mode) and makes a list of them, one per line
    with open('studyious_questions.txt', 'r+') as questionfile:
        questions = questionfile.readlines()
        #Takes off the '\n' from the end of each line so they are neatly presented and easy to read
    questions = [i.strip('\n') for i in questions]

        #reading answers; holds a string only - default value ""; type checking
    ans1 = StringVar()
    ans2 = StringVar()
    ans3 = StringVar()
    ans4 = StringVar()
    ans5 = StringVar()

    answers = []
    for i in range(1,9):
        answers.append(linecache.getline('studyious_answers.txt', i))
    answers = [i.strip('\n') for i in answers]

    def destroy2():
        window1.destroy() #closes window, function attached to '⬅' button

    def answering(answer, index): #The window that appears when the 'create' button is clicked
        toplevel = Toplevel()
        labelCorrectHeader = Label(toplevel, text=txtInfoCorrect, image=imgCorrect) #header graphic
        labelCorrect = Label(toplevel, text=txtInfoCorrect) #text (from variable noted below)
        labelIncorrectHeader = Label(toplevel, text=txtInfoIncorrect, image=imgIncorrect)
        labelIncorrect = Label(toplevel, text=txtInfoIncorrect)
        if answer == answers[index]: #selection structure to decide whether the 'correct' (if) or 'incorrect' (else) window pops up
            labelCorrectHeader.pack()
            labelCorrect.pack()
        else:
            labelIncorrectHeader.pack()
            labelIncorrect.pack()

        #The following two text variable were written using triple quotations so the program would register line breaks effectively
    txtInfoCorrect = """
        Keep up the good work!
        Remember, for every 10 questions, take a lil' break <3 

        """
    txtInfoIncorrect = """
        Better luck next time!
        Keep going, you can do it!

        """

    container01 = Frame(window1)
    container01.pack(anchor = W)

    ButtonQuit = Button(container01, command=destroy2) #FIXME should open the other python file, containing the homepage
    ButtonQuit["text"] = "⬅"
    ButtonQuit.pack(side = LEFT, padx = 10, pady = 8)

        #Directory of images used in this program
    imgCorrect = PhotoImage(file='img_correct.gif')
    imgIncorrect = PhotoImage(file='img_incorrect.gif')
    imgAnswer = PhotoImage(file='btn_answer.gif')
    imgHeaderb = PhotoImage(file='img_header.gif')

    toolbar1 = Canvas()
    Header1 = Label(toolbar1, image = imgHeaderb).pack() #Placing the header image (img_header) at the top of the page
    toolbar1.pack()

        #THE QUESTIONS
    question1 = Label(window1, text=questions[0]).pack(pady = 10) #states the question according to it's number in the list (e.g this one is 0 because it is the first item in the studyious_questions.txt file 
        #pady refers to the space between each of teh questions (so things dont look too cluttered and unprofessional)
    entry1= Entry(window1, textvariable = ans1) #User can cnter answer to to entry box
    entry1.pack()
    ButtonAnswer1 = Button(image = imgAnswer,command=lambda:answering(ans1.get(), 0)).pack() #answer button, when clicked entered answer will be checked against teh answer recorded in studyious_answers.txt to see if it is correct 
        #For example, for the example above, the answer entered by the user is checked against the first answer in the list of answers (index of 0) to see if it correct; promopting a response from the variable 'answering' (which will either produce the 'correct' or 'incorrect dialogue window)

    question2 = Label(window1, text=questions[1]).pack(pady = 10)
    entry2= Entry(window1, textvariable = ans2)
    entry2.pack()
    ButtonAnswer2 = Button(image = imgAnswer,command=lambda:answering(ans2.get(), 1)).pack()

    question3 = Label(window1, text=questions[2]).pack(pady = 10)
    entry3= Entry(window1, textvariable = ans3)
    entry3.pack()
    ButtonAnswer3 = Button(image = imgAnswer,command=lambda:answering(ans3.get(), 2)).pack()

    question4 = Label(window1, text=questions[3]).pack(pady = 10)
    entry4= Entry(window1, textvariable = ans4)
    entry4.pack()
    ButtonAnswer4 = Button(image = imgAnswer,command=lambda:answering(ans4.get(), 3)).pack()

    question5 = Label(window1, text=questions[4]).pack(pady = 10)
    entry5= Entry(window1, textvariable = ans5)
    entry5.pack()
    ButtonAnswer5 = Button(image = imgAnswer,command=lambda:answering(ans5.get(), 4)).pack()

    window1.mainloop()

txtInfo = """
text
"""

txtCreate = """
text
"""

def timetable():
    toplevel = Toplevel() #Ensure window pops up above the main window as a pop up window
    labelTimetableHeader = Label(toplevel, image=imgNote) #header graphic to match aesthetic of the rest of the program
    labelTimetableHeader.pack()
    labelTimetableText = Label(toplevel, text=txtInfo) #The main body of text (as seen above in txtInfo)
    labelTimetableText.pack()

btnQuit = Button(container1,command=destroy) #Quit button, closes window
btnQuit["text"] = "Quit"
btnQuit.pack(side = LEFT, padx = 10, pady = 8) #the difference between the top and left padding is that there is automatically two points of padding from the top of the window

TimetableButton = Button(image = imgTimetable,command=timetable).pack() #a replacable image that allows users to enter place their own timetable on the home page
#Initially the button sat squished in the corner whih made the finishing of the home page look very rough, cluttered and unprofessional

#Button 1; opens a small window giving the user details of how to enter the questions and answers they wish to test themselves on
btnCreate = Button(container2, image = imgCreateBtn,command=create).pack(side = LEFT, padx =100) #padding around teh sides seperates the two buttons - 'create' and 'learn'

#Button 2; redirect to the quiz program, opening another window with the quiz program
btnLearn = Button(container2, image = imgLearnBtn,command=learn).pack(side = BOTTOM, padx = 100)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

您忘了将window1作为任何按钮的主人,如下所示:

ButtonAnswer1 = Button(image = imgAnswer,command=lambda:answering(ans1.get(), 0)).pack()

将window1添加到你的所有ButtonAnswer1-5:

ButtonAnswer1 = Button(window1, image = imgAnswer,command=lambda:answering(ans1.get(), 0)).pack()