簡體   English   中英

通過單擊按鈕在python 3和tkinter中打開新窗口

[英]new window in python 3 and tkinter by clicking on button

這是我現在制作的程序,但是我有問題...當我單擊button1然后打開新窗口時,我該如何制作

import sys
from tkinter import *
import tkinter as tk

def mhello1():
    mlabel = Label(mGui, text='A1').pack()
def mhello2():
    mlabel = Label(mGui, text='A2').pack()
def mhello3():
    mlabel = Label(mGui, text='A3').pack()
def mhello4():
    mlabel
    return
def mAbout():
    messagebox.showinfo(title="About",message="program")
    return
def mQuit():
    mExit = messagebox.askyesno(title="Quit",message="y/n")
    if mExit > 0:
      mGui.destroy()
      return

mGui = Tk()

mGui.geometry('450x450+200+200')
mGui.title('program')
mGui.configure(bg='gray')

mlabel = Label(text='option:',fg='red',bg = 'blue').pack()

mbutton1 = Button(mGui,text ='Button1',command = mhello1, height=5, width=20).pack()
mbutton2 = Button(mGui,text ='Button2',command = mhello2, height=5, width=20).pack()
mbutton3 = Button(mGui,text ='Button3',command = mhello3, height=5, width=20).pack()
mbutton4 = Button(mGui,text ='Button4',command = mhello4, height=5, width=20).pack()
mlabel2 = Label(text='activity:',fg='red',bg = 'blue').pack()

menubar=Menu(mGui)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="qwer")
filemenu.add_command(label="quit",command = mQuit)
menubar.add_cascade(label="more options",menu=filemenu)

helpmenu = Menu(menubar, tearoff = 0)
helpmenu.add_command(label="Help Docs")
helpmenu.add_command(label="About", command = mAbout)
menubar.add_cascade(label="help",menu=helpmenu)
mGui.config(menu=menubar)

mGui.mainloop()

我嘗試了該程序,但是它不起作用: Python 3和tkinter通過單擊按鈕打開新窗口

有沒有一種方法我不使用tkinter頂級?

Tnx很多:)

由於您只應創建一個根窗口,因此必須使用“ 頂層”打開一個新的根窗口。

def mhello1():
    toplevel = Toplevel()
    toplevel.title('Another window')
    toplevel.focus_set()

如果您想使用消息框,請在下面使用這些行

from tkinter import *
from tkinter import messagebox

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM