使用 tkinter 进行 Python GUI 编程

我有一个有多行的表单。按 Enter 键会打开一个选项窗口。但每次我按回车键时,它都会打开一个新的选项窗口。我如何控制和检查选项窗口是否打开然后不打开选项窗口。

import tkinter as tk

from tkinter import ttk

from tkinter import *

from tkinter import messagebox


# Directory/File processing libraries

import os

import configparser

import csv


def callback():

    #messagebox.showinfo("Netezza", Folder_Name_var.get())

    #messagebox.showinfo("Netezza", Table_Name_var.get() )

    config = configparser.ConfigParser()

    config.read('C:\\aa\\config.ini')

    #for value in config['Folder']: print(value)

    for key in config.items('Folder'):

        print (key[1].replace('{Folder_Name}',Folder_Name_var.get()))

        os.makedirs(key[1].replace('{Folder_Name}',Folder_Name_var.get()),exist_ok=True)


def click_tv(event):

    #messagebox.showinfo("Inside")

    selected=trv.focus()

    print(trv.item(selected))


def press_enter(event):

    #messagebox.showinfo("Inside")

    selected=trv.focus()

    print(trv.item(selected))

    print(str((event.keysym)))

    if str((event.keysym))=='Return':

        option_wnd=Toplevel(root)

        option_wnd.geometry('200x200')

        option_wnd.title('Option Window')

        option_wnd.grab_set()

        #option_wnd.pack()

def selection_change(event):

    selected = trv.selection()[0]

    print('You clicked on', trv.item(selected))

    #option_wnd.mainloop()

root = Tk()


Folder_Name_var = tk.StringVar()

Table_Name_var = tk.StringVar()


# This is the section of code which creates the main window

root.geometry('873x498')

root.configure(background='#63B8FF')

root.title('Automation Software - Blue Shield of California')


pic= Canvas(root, height=100, width=100)

#pic= Canvas(root, height=225, width=580)


#picture_file = PhotoImage(file = 'c:\\aa\\bsc.png')

#pic.create_image(0, 0, anchor=NW, image=picture_file)

#pic.place(x=5, y=5)

每次我在根窗口上按 Enter 键时,都会打开一个弹出窗口。我需要控制它。如果弹出窗口打开,那么我们不应该允许另一个弹出窗口打开。

https://img1.sycdn.imooc.com/65b4bcb70001e82d06500360.jpg

婷婷同学_
浏览 55回答 4
4回答

繁华开满天机

直接的方法是使用winfo_exists():root.option_wnd = None # Init valuedef press_enter(event):    #messagebox.showinfo("Inside")    selected=trv.focus()    print(trv.item(selected))    print(str((event.keysym)))    if str((event.keysym))=='Return':         if root.option_wnd and root.option_wnd.winfo_exists():            root.option_wnd.lift() # make this window on the top.        else: # create this window            root.option_wnd  = tk.Toplevel(root)            .....但我认为你不需要每次用户输入时都创建这个窗口。Enter在开始时创建它,只需在用户输入时显示它Enter例如:root = tk.Tk()option_wnd = tk.Toplevel()option_wnd.wm_protocol("WM_DELETE_WINDOW", option_wnd.withdraw) # when user try to close this window, hide it instead of destroy it.....option_wnd.withdraw() # hide this windowdef press_enter(event):    #messagebox.showinfo("Inside")    selected=trv.focus()    print(trv.item(selected))    print(str((event.keysym)))    if str((event.keysym))=='Return':         option_wnd.deiconify() # show it.

缥缈止盈

我能想到的一个选项是像这样绑定选项窗口:option_wnd.bind('<Return>',&nbsp;lambda&nbsp;e:&nbsp;option_wnd.close())&nbsp;#&nbsp;or&nbsp;withdraw()&nbsp;or&nbsp;quit()&nbsp;?将选项窗口绑定到按下 Enter 键时它会关闭(尽管我不知道上述函数的差异(有,所以你应该查找)),但是如果你想使用Enter(返回)输入值,这可能会妨碍你键,因为它将关闭窗口。另一个选项是将选项窗口绑定到此事件:option_wnd.bind('<FocusOut>',&nbsp;lambda&nbsp;e:&nbsp;option_wnd.close())这是当窗口不再受到关注时,因此如果您按 Enter 键,它将打开一个新窗口,但仍然打赌旧窗口应该关闭。您也可以尝试用某些东西进行一些逻辑编程,例如当输入按下“打开”模式时,再次按下它不会打开窗口,而当您关闭现有窗口时,它将再次允许这种情况。

ITMISS

def press_enter(event):&nbsp; &nbsp; #messagebox.showinfo("Inside")&nbsp; &nbsp; root.deiconify ()&nbsp; &nbsp; selected=trv.focus()&nbsp; &nbsp; print(trv.item(selected))&nbsp; &nbsp; print(str((event.keysym)))&nbsp; &nbsp; if str((event.keysym))=='Return':&nbsp; &nbsp; &nbsp; &nbsp; option_wnd=Toplevel(root)&nbsp; &nbsp; &nbsp; &nbsp; option_wnd.geometry('200x200')&nbsp; &nbsp; &nbsp; &nbsp; option_wnd.title('Option Window')&nbsp; &nbsp; &nbsp; &nbsp; option_wnd.grab_set()&nbsp; &nbsp; &nbsp; &nbsp; #option_wnd.pack()

慕运维8079593

如果您不使用类,您可以执行以下操作:options_displayed = False #globaldef option_closed(w):&nbsp; &nbsp; global options_displayed&nbsp; &nbsp; w.destroy() #close the actual window&nbsp; &nbsp; options_displayed = False # log the fact it is now closedef press_enter(event):&nbsp; &nbsp; global options_displayed # reference global variable&nbsp; &nbsp; #messagebox.showinfo("Inside")&nbsp; &nbsp; selected=trv.focus()&nbsp; &nbsp; print(trv.item(selected))&nbsp; &nbsp; print(str((event.keysym)))&nbsp; &nbsp; if str((event.keysym))=='Return' and not options_displayed:&nbsp; &nbsp; &nbsp; &nbsp; option_wnd=Toplevel(root)&nbsp; &nbsp; &nbsp; &nbsp; option_wnd.geometry('200x200')&nbsp; &nbsp; &nbsp; &nbsp; option_wnd.title('Option Window')&nbsp; &nbsp; &nbsp; &nbsp; option_wnd.grab_set()&nbsp; &nbsp; &nbsp; &nbsp; option_wnd.grab_set()&nbsp; &nbsp; &nbsp; &nbsp; option_wnd.protocol("WM_DELETE_WINDOW", lambda w= option_wnd :option_closed(w))&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python