如何从顶层窗口到主窗口获取变量? [英] How to get variables from toplevel window to main window?

查看:25
本文介绍了如何从顶层窗口到主窗口获取变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个获取键输入并将其显示在主窗口顶层窗口的输入框中的代码.在主窗口中,我有一个列表框,我希望在顶层按下确认按钮时获取显示在输入框上的键输入.

I have a code that gets keyinputs and show it on a entrybox in the toplevel window of the main window. In the main window I have a listbox and I am wishing to get the keyinput that is shown on the entrybox to be enlisted when the confirm button is pressed on toplevel.

我尝试了多种方法将 evt.keysym 放入我的列表框,但都失败了.

I tried several ways to get evt.keysym to my listbox but all failed.

class EntryBox(tk.Entry):
    def __init__(self, master, cnf = {}, **kw):
        kw = tk._cnfmerge((kw, cnf))
        kw['justify'] = kw.get('justify', 'center')
        kw['width'] = 15
        kw['state'] = 'readonly'
        super(EntryBox, self).__init__(master=master, **kw)
        self.unbind_class('Entry', '<BackSpace>')
        self.unbind_class('Entry', '<Key>')
        self.bind_class(self, '<Key>', self._display)

    def _display(self, evt):
        self['state'] = 'normal'
        self.delete('0', 'end')
        self.insert('0', str(evt.keysym))
        self['state'] = 'readonly'

class Keyboard:
    def __init__(self):
        self.kb = tk.Toplevel()

        kb_frame = ttk.Frame(self.kb)
        kb_frame.grid(column=0, row=1, pady=(7, 19))
        ttk.Label(kb_frame, text='Enter Key').grid(column=0, row=0, pady=4)
        entry = EntryBox(kb_frame)
        entry.grid(column=0, row=1)

        # Confirm button
        self.co_button = ttk.Button(self.kb, text='Confirm')
        self.co_button.grid(column=0, row=2)

class Main:
    def __init__(self):
        self.win = tk.Tk()
        # listbox
        lb_frame = tk.Frame(self.win)
        lb_frame.grid(column=0, row=0)
        scrollbar = tk.Scrollbar(lb_frame, orient='vertical')
        scrollbar.grid(column=1, row=0, sticky='NS', pady=(12, 4))
        listbox = tk.Listbox(lb_frame, selectmode='extended', width=25, 
                             height=16,
                             yscrollcommand=scrollbar.set, activestyle='none')
        listbox.grid(column=0, row=0, sticky='NSEW', padx=(6, 0), pady=(12, 4))
        scrollbar.config(command=listbox.yview)

        # button to open toplevel
        bt_frame = ttk.Frame(self.win)
        bt_frame.grid(column=2, row=0, rowspan=2)

        self.kb_button = ttk.Button(bt_frame, text='KeyBoard', command=KeyBoard)
        self.kb_button.grid(column=0, row=0)

main = Main()
main.win.mainloop()

推荐答案

要将值从一个类获取到另一个类,您必须将它们链接起来.将 Widgets 直接继承到类将有助于您在 Tk() 窗口和 Toplevel() 窗口之间建立连接.

To get values from one class to another class you've to link them. Inheriting the Widgets directly to the class will help you a lot in establishing a connection between Tk() window and Toplevel() Window.

Keyboard 窗口已经打开时的另一件事是通过 configure state = 'disabled' 禁用按钮,这样用户就不会错误地打开另一个窗口,当Keyboard 窗口关闭,通过 state = 'normal' 重新启用按钮.

One more thing when a Keyboard window is already opened disable the button by configure state = 'disabled' so the user won't open another one by mistake and when Keyboard window is closed re-enable the button by state = 'normal'.

完整代码如下:

import tkinter as tk
import tkinter.ttk as ttk

class EntryBox(tk.Entry):
    def __init__(self, master, cnf = {}, **kw):
        kw = tk._cnfmerge((kw, cnf))
        kw['justify'] = kw.get('justify', 'center')
        kw['width'] = 15
        kw['state'] = 'readonly'
        super(EntryBox, self).__init__(master=master, **kw)
        self.bind_class(self, '<Key>', self._display)

    def _display(self, evt):
        self['state'] = 'normal'
        self.delete('0', 'end')
        self.insert('0', str(evt.keysym))
        self['state'] = 'readonly'


class Keyboard(tk.Toplevel):
    def __init__(self, master=None, cnf={}, **kw):
        super(Keyboard, self).__init__(master=master, cnf=cnf, **kw)
        self.master = master
        kb_frame = ttk.Frame(self)
        kb_frame.grid(column=0, row=1, pady=(7, 19))
        ttk.Label(kb_frame, text='Enter Key').grid(column=0, row=0, pady=4)
        self.entry = EntryBox(kb_frame)
        self.entry.grid(column=0, row=1)

        # This protocol calls the function when clicked on 'x' on titlebar
        self.protocol("WM_DELETE_WINDOW", self.Destroy)

        # Confirm button
        self.co_button = ttk.Button(self, text='Confirm', command=self.on_press)
        self.co_button.grid(column=0, row=2)

    def on_press(self):
        key = self.entry.get()
        # Condition to not have duplicate values, If you want to have duplicate values remove the condition
        if key not in self.master.lb.get('0', 'end') and key:
            # Insert the Key to the listbox of mainwindow
            self.master.lb.insert('end', key)

    def Destroy(self):
        # Enable the button
        self.master.kb_button['state'] = 'normal'
        # Then destroy the window
        self.destroy()

class Main(tk.Tk):
    def __init__(self):
        super(Main, self).__init__()

        bt_frame = ttk.Frame(self)
        bt_frame.grid(column=2, row=0, rowspan=2)

        self.kb_button = ttk.Button(bt_frame, text='KeyBoard', command=self.on_press)
        self.kb_button.grid(column=0, row=0)

        self.lb = tk.Listbox(bt_frame)
        self.lb.grid(column=0, row=1)

    def on_press(self):
        # Creating a toplevel window and passed self as master parameter
        self.Kb = Keyboard(self)
        # Disabled the button
        self.kb_button['state'] = 'disabled'


main = Main()
main.mainloop()

这篇关于如何从顶层窗口到主窗口获取变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆