如何让按钮打开一个新窗口?

时间:2015-03-03 01:57:58

标签: python user-interface tkinter

我正在制作一个以主菜单开头的简单GUI,用户可以点击按钮进入一个新窗口,其中有一个键盘图片,用户可以按键盘上的键来播放画面。现在我无法弄清楚如何按下按钮关闭主菜单(标记为mainMenu())并打开游戏菜单(playGame)。

import tkinter
from tkinter import *

class mainMenu:
    def _init_(self, master):
        frame = Frame(master)
        frame.pack()

        self.quitButton = Button(frame, text = "Quit", command = frame.quit)
        self.quitButton.pack(side = LEFT)

        self.proceedButton = Button(frame, text = "Play", command = playGame)
        self.proceedButton.pack(side = LEFT)

        def playGame(self):
            frame.quit
            gameMenu()


def gameMenu(self):
    root = Tk()
    b = mainMenu(root)

    topFrame = Frame(root)
    topFrame.pack()
    bottomFrame = Frame(root)
    bottomeFrame.pack(side = BOTTOM)
    photo = PhotoImage(file = "piano.png")
    label = Label(root, image = photo)
    label.pack()

root.mainloop()

1 个答案:

答案 0 :(得分:0)

你必须原谅我删除你的课程,但我以前从未亲自在python中使用过类。但是我似乎让你的代码在某种程度上起作用。

import tkinter
from tkinter import *

def playGame():
    frame.quit
    gameMenu()


def gameMenu():
    b = mainMenu(root)

    topFrame = Frame(root)
    topFrame.pack()
    bottomFrame = Frame(root)
    bottomFrame.pack(side = BOTTOM)
    photo = PhotoImage(file = "piano.png")
    label = Label(root, image = photo)
    label.pack()

root=Tk()
frame = Frame(root)
frame.pack()

quitButton = Button(frame, text = "Quit", command = frame.quit)
quitButton.pack(side = LEFT)

proceedButton = Button(frame, text = "Play", command = playGame)
proceedButton.pack(side = LEFT)

root.mainloop()

您遇到的主要问题是您同时使用rootmaster。在tkinter中声明主窗口时,您通常使用root = Tk()master = Tk(),其中一个是可接受的,我个人使用master。此变量包含放置其他所有内容的主窗口。您还没有将Tk()放入任何变量,这意味着当您点击root.mainloop()时没有任何内容可以进入主循环,这是因为您试图在gameMenu中声明root = Tk(),在你的程序中没有被调用。

如果你想在tkinter中打开窗口,可能更容易编写这样的东西:

from tkinter import *

master = Tk() #Declaring of main window

def ProceedButtonCommand(mainframe, master): #Command to attach to proceed button
    mainframe.destroy()
    DrawSecondScreen(master) #This line is what lets the command tied to the button call up the second screen

def QuitButtonCommand(master):
    master.destroy()

def DrawFirstScreen(master):
    mainframe = Frame(master) #This is a way to semi-cheat when drawing new screens, destroying a frame below master frame clears everything from the screen without having to redraw the window, giving the illusion of one seamless transition
    ProceedButton = Button(mainframe, text="Proceed", command=lambda: ProceedButtonCommand(mainframe, master)) #Lambda just allows you to pass variables with the command
    QuitButton = Button(mainframe, text = "Quit", command=lambda: QuitButtonCommand(master))
    mainframe.pack()
    ProceedButton.pack()
    QuitButton.pack()

def DrawSecondScreen(master):
    mainframe = Frame(master)
    Label1 = Label(mainframe, text="Temp")
    mainframe.pack()
    Label1.pack()

DrawFirstScreen(master)
master.mainloop() #The mainloop handles all the events that occur in a tkinter window, from button pressing to the commands that a button runs, very important

这个小脚本只是绘制一个带有两个按钮的屏幕,一个绘制一个带有文本“temp”的新屏幕,另一个按钮关闭master窗口。 在将来,要求有编程经验的朋友帮助解决这类问题可能是个更好的主意。在一些计算论坛上讨论,我相信你会找到一组快速共享和修复代码。

相关问题