简体   繁体   English

如何使按钮打开新窗口?

[英]How to make a button open a new window?

I am making a simple GUI that starts with a main menu them the user can click a button to proceed to a new window which has a picture of a keyboard and the user can press key on their keyboard to play the paino. 我正在制作一个简单的GUI,该GUI从主菜单开始,用户可以单击按钮以进入带有键盘图片的新窗口,并且用户可以按键盘上的键来播放痛苦。 Right now I cant figure out how to make a button that when pressed closes the main menu (labeled mainMenu()) and open the game menu (playGame). 现在,我不知道如何制作一个按钮,当按下该按钮时,它会关闭主菜单(标记为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()

You'll have to forgive me for removing your class but I've never personally worked with classes in python before. 您必须原谅我删除您的类,但是我以前从未亲自使用python中的类进行过工作。 However I seem to have you code working to some degree. 但是我似乎让您的代码在某种程度上可以正常工作。

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

The main problem you had was that you were using both root and master . 您遇到的主要问题是您同时使用rootmaster When declaring the main window in tkinter you normally use either root = Tk() or master = Tk() either one is acceptable, personally I use master . 在tkinter中声明主窗口时,通常使用root = Tk()master = Tk()之一都可以接受,我个人使用master This variable contains the main window that everything else is placed into. 此变量包含其他所有内容放置的主窗口。 You also hadn't put Tk() into any variable, meaning that when you hit root.mainloop() there was nothing to enter the main loop, this was because you were trying to declare root = Tk() inside gameMenu, which wasn't getting called in your program. 您也没有将Tk()放入任何变量,这意味着当您击中root.mainloop()时,没有任何内容可以进入主循环,这是因为您试图在gameMenu中声明root = Tk() ,在您的程序中不会被调用。

If you want to open windows within tkinter it's probably easier to write something like this: 如果您想在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

This little script just draws a screen with two buttons, one draws a new screen with the text "temp" on it and the other button closes the master window. 这个小脚本仅绘制一个带有两个按钮的屏幕,一个绘制一个带有文本“ temp”的新屏幕,另一个按钮关闭master窗口。 In the future it's probably a better idea to ask a friend who is experienced in programming to help with this kind of stuff. 将来,最好找一个有编程经验的朋友来帮助这类事情。 Get talking on some computing forums, I'm sure you'll find a group of sharing and fixing code quickly. 在一些计算论坛上聊天,我相信您会很快找到一组共享和修复代码的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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