【问题标题】:tkinter Button does not open new windowtkinter 按钮不打开新窗口
【发布时间】:2017-06-14 20:58:21
【问题描述】:

我在使用 tkinter 时遇到问题。 我的代码旨在在按下按钮时打开一个新窗口,但该窗口没有打开。

这是我的代码:

主模块

#!/usr/bin/python
#encoding: latin-1
import tkinter
import ce

#window config
window = tkinter.Tk()                   #create window
window.title("BBDOassist")              #set title
window.geometry("750x500")              #set size

…

#   buttons
button_ce = tkinter.Button(window, text="CE Evaluation", command="ce.run()")
button_ce.pack()


window.mainloop()                       #draw the window and start

“CE”模块

#!/usr/bin/python
#encoding: latin-1
import tkinter

…

def run():
  #window config
    window = tkinter.Tk()                                   #create window
    window.title("BBDOassist - CE Evaluation")              #set title
    window.geometry("750x500")                              #set size

…

    window.mainloop()                                    #draw the window and start

【问题讨论】:

    标签: python-3.x tkinter window


    【解决方案1】:

    你至少有两个问题

    首先,您必须为command 属性提供对函数的引用。您正在向它传递一个字符串。字符串是无用的。您需要将按钮定义更改为:

    button_ce = tkinter.Button(window, text="CE Evaluation", command=ce.run)
    

    其次,如果你想创建额外的窗口,那么你需要创建Toplevel 的实例而不是Tk。一个 tkinter 程序只需要一个 Tk 的实例,并且你需要只调用一次 mainloop

    run 更改为如下所示(并在run 中删除对mainloop 的调用):

    def run():
        #window config
        window = tkinter.Toplevel()
        ...       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 2013-02-07
      • 2021-02-15
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多