如何在单击子窗口时打开菜单按钮禁用菜单按钮,直到子窗口在 tkinter 中关闭

问题描述

我在父窗口上创建了一个菜单栏。我制作了一个子窗口作为 userguide.py,它被分配给菜单上的“用户指南”按钮之一。当我单击该按钮时,子窗口会打开,但再次单击时,它会打开另一个窗口,如果单击菜单按钮,则会继续。如何在子窗口关闭之前禁用菜单按钮。我正在分享下面的代码

下面是父窗口菜单代码

from tkinter import *
from tkinter import ttk
from userguide import userGuide 

root = Tk()
root.title("GhostTube - by rusher")
root.iconbitmap(r"C:\Users\ayush\Desktop\BOTRAW\main\ghost.ico")
root.maxsize(400,685)
root.minsize(400,685)
root.geometry("400x685")

style = ttk.Style()
style.theme_use("clam")
menubar = Menu(root) 
menubar.add_command(label="USER GUIDE    I",command=userGuide)
menubar.add_command(label="ABOUT    I")
menubar.add_command(label="CONTACT",command=contactInfo)

root.config(menu=menubar)
root.mainloop()`

Menu bar image

下面给出的代码是子窗口代码,即userguide.py

from tkinter import *
from tkinter import ttk

def userGuide():

    master = Tk()
    master.title("GhostTube - by rusher")
    master.iconbitmap(r"C:\Users\ayush\Desktop\BOTRAW\main\ghost.ico")
    master.maxsize(400,685)
    master.minsize(400,685)
    master.geometry("400x685")

    style = ttk.Style()
    style.theme_use("clam")


    userLabel = Label(master,text="                  HOW TO USE                  ",bg="black",fg="white",font=("Elephant",18))
    userLabel.grid(pady=10)

    guide1Frame = LabelFrame(master,text="STEP 1 :",padx=5,pady=10)
    guide1Frame.grid(row=1,column=0,padx=5)

    step1label = Label(guide1Frame,text="First step is to add accounts to the bot,for that click on \n“Load Accounts” then locate the “YT accounts.txt” file and select it.\nIt will load all the accounts in the file.")
    step1label.grid()

    guide2Frame = LabelFrame(master,text="STEP 2 :",pady=10)
    guide2Frame.grid(row=2,padx=5)

    step2label = Label(guide2Frame,text="Add the video URL of which you want to boost. Type or copy paste \nyour video URL on (Type Here) space and click on “Add”. \nor you can make a text file of your video URLs and load it \nby clicking on “Load Video URLs” and selecting the text file.")
    step2label.grid()

    guide3Frame = LabelFrame(master,text="STEP 3 :",pady=10)
    guide3Frame.grid(row=3,padx=5)

    step3label = Label(guide3Frame,text="Now you need to select the action you want to have \non the video to be performed,that are: LIKE,disLIKE,\nSUBSCRIBE,UNSUBSCRIBE or COMMENT. You can select multiple \nactions at a time. After completing this Click on “START” button.")
    step3label.grid()

    guide4Frame = LabelFrame(master,text="STEP 4 :",pady=10)
    guide4Frame.grid(row=4,padx=5)

    step4label = Label(guide4Frame,text="For Comments,You can Type or copy paste comments on\n(Type Here) of your choice,one comment at a time then click\non “Add” button and then type next comment of your choice then\nclick on “Add” and repeat if you want more. Or you can make\na text file of comments of your choice (one comment per line)\nand load it by clicking on “Load Comments”and locate your\ncomments text file and select it. Then click on “START”.")
    step4label.grid()

    guide5Frame = LabelFrame(master,text="STEP 5 :",pady=10)
    guide5Frame.grid(row=5,padx=5)

    step5label = Label(guide5Frame,text="When the bot runs first time it can take time because of it prepares\nthe system,after the process starts please do not intercept it.\nLet it complete all the actions. The process goes in steps; \nfirst,a CMD interface will open then a Firefox window will start and\nit will complete one action then the Firefox window will close and\nthe same steps will repeat according to the actions to be taken.")
    step5label.grid()

    quote = Frame(master,pady=10)
    quote.grid(row=6,columnspan=2)

    enjoy = Label(quote,text="Experience The Best",16),fg="white")
    enjoy.grid(padx=5)

    master.mainloop()

请帮帮我,因为我是这个话题的新手。

解决方法

尝试这样的事情:

import tkinter as tk

def create_window(root):
    # Create the new window
    top = tk.Toplevel(root)
    # Stop the user from interacting with the main window
    top.grab_set()

root = tk.Tk()

menubar = tk.Menu(root) 
menubar.add_command(label="Create new window",command=lambda: create_window(root))
root.config(menu=menubar)

root.mainloop()

它使用 top.grab_set() 来阻止用户与主窗口交互。它会阻止所有事件,直到用户关闭弹出窗口。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...