【问题标题】:Tkinter - Multiple windows openingTkinter - 打开多个窗口
【发布时间】:2019-08-07 03:01:42
【问题描述】:

就像标题所说的那样,我在打开多个窗口时遇到了问题。每次单击按钮时都会出现新窗口(同一窗口)。我一直无法找到这个特定场景的答案。我什至尝试在打开窗口后禁用按钮(这导致了重新启用的问题)。

下面是足够的代码,您可以通过单击按钮几次来查看问题。我希望有一个简单的解决方案,因为我对 Tkinter 还很陌生。此外,较小的窗口需要与主窗口一起调整(扩展)大小,所以我认为顶级窗口不起作用。

可能其他人也有同样的问题,在此先感谢!

注意:我使用的是 Python 2.7 (Tkinter)

#!/usr/bin/python

import os
import sys

import Tkinter as tk
from Tkinter import *
from ScrolledText import *
import tkFileDialog
import tkMessageBox

# Main
root = tk.Tk(className = "tex")
root.geometry("500x300")
root.title("tex")
tex = ScrolledText(root, padx=2, pady=2, undo=True, font=('Arial 11'))

def note_area():
    btn_frame = Frame()
    note = LabelFrame(tex, bd=1, relief='ridge')

    tx = Text(note, width=18, relief='flat', padx=2, pady=2)
    tx.insert('1.0', "Notes..")
    tx.pack(side='top', fill=BOTH, expand=True)

    note.pack(side='right', fill=Y)
    btn_frame.pack(side='bottom', fill=Y)

# ToolBar Button  (should only open one instance of note_area)
toolbar = Frame(root, bd=2, relief='groove')
b4 = Button(toolbar, text="Notes", width=4, command=note_area)
b4.pack(side=RIGHT, padx=4, pady=2)
toolbar.pack(side=TOP, fill=X)

tex.pack(fill="both", expand=True)
root.mainloop()  

【问题讨论】:

    标签: python-2.7 tkinter


    【解决方案1】:

    您只需要编写一个变量来跟踪您是否打开了笔记窗口。

    tex.notes_open = False
    def note_area():
        if tex.notes_open: 
            return # abort the function, notes already open
        else:
            tex.notes_open = True # set the flag for the next time
            # rest of your code
    

    【讨论】:

    • 如果我关闭笔记窗口,我将无法再次打开它。有什么诀窍吗?
    猜你喜欢
    • 2013-09-18
    • 2018-05-11
    • 2018-11-17
    • 1970-01-01
    • 2015-10-09
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多