尝试在 pygame 中使用来自 tk 的保存对话时会打开随机 tk 窗口

编程入门 行业动态 更新时间:2024-10-28 10:35:03
本文介绍了尝试在 pygame 中使用来自 tk 的保存对话时会打开随机 tk 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

每当我单击应用程序上的保存按钮时,它都会打开一个正常工作的保存对话框和一个在我尝试关闭它时立即崩溃的 tkinter 窗口.这是我以某种方式生成随机窗口的代码:

Whenever I click the save button on my app, it opens a save dialogue that works correctly and a tkinter window that crashes as soon as I try to close it. Here is my code that somehow makes the random window:

import tkFileDialog,pygame

pygame.init()

screen = pygame.display.set_mode((640,360))

pygame.display.set_caption("Idea Processor")

# code is cut here

try:
    ideasetupfonts = pygame.font.SysFont("Ubuntu", 36, False, False)
except:
    ideasetupfonts = pygame.font.SysFont("Arial", 36, False, False)
# TextNameToBlit = ideasetupfonts.render(" TEXT HERE ",1,(0,0,0))

Potato = True
ShowToolbar = True
newSaveFile = {"Hello World":(50,50)}



def SaveFile():
    filename = tkFileDialog.asksaveasfilename(**{"title":"Save Idea...","defaultextension":".txt","filetypes":[("text files", ".txt")],})
    if filename:
        saveFile = open(filename, 'w')
        print >>saveFile,newSaveFile
        saveFile.close()

while Potato:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Potato = False
    screen.fill(white)

    # and here

    mousex,mousey = pygame.mouse.get_pos()

    # SaveFile button
    if mousex>=0 and mousex<=32 and mousey>=0 and mousey<=32 and pygame.mouse.get_pressed() == (True, False, False) and ShowToolbar:
        SaveFile()


    # and here

    pygame.display.flip()

pygame.quit()

推荐答案

该窗口是默认 Tk 元素,如果您尚未创建前一个窗口小部件,则在创建 Tkinter 小部件时创建该窗口.这是因为 tkFileDialog 建立在 Tkinter 之上.我建议您自己创建该元素并通过调用其 withdraw 方法将其隐藏.

That window is the default Tk element that is created when a Tkinter widget is created if you haven't created a previous one. This happens because tkFileDialog is built on top of Tkinter. I suggest you to create that element yourself and hide it by calling its withdraw method.

import Tkinter, tkFileDialog, pygame

root = Tkinter.Tk()
root.withdraw()

<小时>

顺便说一下,我已经看到您已将 Pygame 循环的标志命名为 Potato.我不知道它与此变量的实际使用有何关系,但我强烈建议您为类、变量和模块使用有意义的名称.这是Object Mentor 的一篇文章(这是优秀书籍的一部分)Clean Code") 专门关于命名,我希望你觉得它有用.


As a side note, I have seen that you have named the flag for your Pygame loop Potato. I don't know how it could be related with the real use of this variable, but I strongly recommend you to use meaningful names for your classes, variables and modules. Here it is an article from Object Mentor (which is part of the excellent book "Clean Code") exclusively about naming, I hope you find it useful.

这篇关于尝试在 pygame 中使用来自 tk 的保存对话时会打开随机 tk 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 07:04:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390829.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时会   窗口   pygame   tk

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!