拆下Tkinter信息框后面的屏幕

2024-04-25 00:29:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想显示一个消息框,但是在Python中它后面没有父窗口。这是我的代码:

import Tkinter, tkFileDialog ,tkMessageBox
from fileManagerModule import fileManager
def load():
    global myFile,flag,msg
    flag=True
    options = {}
    options["title"] = "choose a text File"
    options['initialdir'] = '~/'
    fileName = tkFileDialog.askopenfilename(**options)
    myFile = fileManager(fileName)
    myText.delete("1.0", Tkinter.END)
    try:
        line = myFile.readFromFile()
        myText.insert("1.0", line)
    except:
        msg=Tkinter.Tk()
        msg=tkMessageBox.showerror("Error","please choose file to load")

screenshot


Tags: import消息tkinterlineloadmsgfilenamemyfile
2条回答

您可以使用withdraw()函数删除在后台显示的window,只显示对话框。在

试试这个:

import Tkinter, tkFileDialog ,tkMessageBox
from fileManagerModule import fileManager
def load():
    global myFile,flag,msg
    flag=True
    options = {}
    options["title"] = "choose a text File"
    options['initialdir'] = '~/'    
    fileName = tkFileDialog.askopenfilename(**options)
    myFile = fileManager(fileName)
    myText.delete("1.0", Tkinter.END)
    try:
        line = myFile.readFromFile()
        myText.insert("1.0", line)
    except:
        msg=Tkinter.Tk()

        msg.withdraw()

        msg=tkMessageBox.showerror("Error","please choose file to load")

在代码中添加以下内容:

import Tkinter,tkMessageBox

一。 . 在程序初始化时添加一个全局变量

^{pr2}$

在代码初始化时,添加以下行: .. 在第一次调用tkMessageBox之前

def initialise():
    global msgWindow
    .
    .
    msgWindow = Tkinter.Tk()
    msgWindow.withdraw()

在调用tkMessageBox的代码中,只需调用 tkMessageBox就是你通常所说的。在

if not tkMessageBox.askyesno('What to do','Continue ?'):

。。。。。在

在程序终止的任何退出点,添加

 msgWindow.destroy()

那就行了。在

相关问题 更多 >