代码之家  ›  专栏  ›  技术社区  ›  Hoseong Jeon

Python Tkinter窗口未显示-Python 3[重复]

  •  0
  • Hoseong Jeon  · 技术社区  · 7 年前

    我正在使用Python 3,但我遇到了一个问题。

    我做了一个测试项目,但不起作用。

    这是我的代码:

    import pygame, sys, os
    from pygame.locals import *
    from tkinter import *
    
    class A(object):
        def Start(self):
            self.root = Tk()
    
            self.btn1 = Button(self.root, text = '1', command = self.one())
            self.btn1.grid(row = 0, column = 0)
    
            self.btn2 = Button(self.root, text = '2', command = self.two())
            self.btn2.grid(row = 0, column = 1)
    
            self.root.mainloop()
    
        def one(self):
            pygame.quit()
    
        def two(self):
            sys.exit()
    
    while True:
        a = A()
        a.Start()
        os.execl(sys.executable, os.path.abspath(__file__), *sys.argv)
    

    有什么问题吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Hoseong Jeon    7 年前

    哦,我想我自己找到了答案。

    我必须改变 self.one() self.two() self.one self.two .

    我必须修改一些代码才能做到。

    答案如下:

    import pygame, sys, os
    from pygame.locals import *
    from tkinter import *
    
    class A(object):
        def Start(self):
            self.root = Tk()
    
            self.btn1 = Button(self.root, text = '1', command = self.one)
            self.btn1.grid(row = 0, column = 0)
    
            self.btn2 = Button(self.root, text = '2', command = self.two)
            self.btn2.grid(row = 0, column = 1)
    
            self.root.mainloop()
    
        def one(self):
            self.root.destroy()
    
        def two(self):
            self.root.destroy()
            pygame.quit()
            sys.exit()
    
    def START():
        a = A()
        a.Start()
    
    while True:
        START()
        os.execl(sys.executable, os.path.abspath(__file__), *sys.argv)