【问题标题】:tkinter converted to exe not openingtkinter 转换为 exe 无法打开
【发布时间】:2020-08-11 12:31:11
【问题描述】:

我正在使用 tkinter 制作一些简单的程序,它允许我更改我的计算机分辨率。它在直接运行它时认为 python IDLE 运行良好,但是当我使用 auto-py-to-exe 和 py-installer 将其转换为 EXE 时,tkinter 窗口不会打开并且命令提示符会暂时打开直到关闭本身。 起初我以为是我的代码或模块,因为我的代码使用了 winapi,但我在我制作的另一个 tkinter 上尝试了它,但这也无法打开转换后的 tkinter exe。

import win32api
import win32con
import pywintypes
from tkinter import *

def quit():
    main_window.destroy()

def main():
    global option
    option = StringVar()
    
    main_window.geometry("300x200")
    main_window.title("change resolution")

    selected_label = Label(main_window, textvariable = option, font = 'Arial 15 bold')
    selected_label.grid(column=0, row=0)

    choose_label = Label(main_window, text = "Please choose an option")
    choose_label.grid(column=0, row=1)

    defaultbutton = Button(main_window, text= 'default', command = default)
    defaultbutton.grid(column=0, row=2)
    teamviewerbutton = Button(main_window, text= 'teamviewer', command = teamviewer)
    teamviewerbutton.grid(column=1, row=2)

    quitbutton = Button(main_window, text = 'quit', command = quit)
    quitbutton.grid(column=1, row=0)

    
    

def default():
    option.set("default")
    
    devmode = pywintypes.DEVMODEType()

    devmode.PelsWidth = 3440
    devmode.PelsHeight = 1440

    devmode.Fields = win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT

    win32api.ChangeDisplaySettings(devmode, 0)


def teamviewer():
    option.set("teamviwer")

    devmode = pywintypes.DEVMODEType()

    devmode.PelsWidth = 1920
    devmode.PelsHeight = 1080
    devmode.Scale = 2

    devmode.Fields = win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT

    win32api.ChangeDisplaySettings(devmode, 0)

main_window = Tk()


main()

【问题讨论】:

  • 在末尾添加 main_window.mainloop()
  • 如果有效,请务必提供答案!这对其他人会有所帮助

标签: python winapi tkinter


【解决方案1】:

感谢 Rahul 的回答,答案很简单,距离我上次使用它已经有一段时间了。

main_window.mainloop()

or

[root tk].mainloop()

这应该放在代码的底部,就在 main() 的正下方。

【讨论】:

  • 很高兴您得到了解决方案,感谢您的分享,如果您将它们标记为答案,我将不胜感激,这将对其他社区有益。
猜你喜欢
  • 2018-06-26
  • 2019-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
相关资源
最近更新 更多