打开窗口和焦点文本框tkinter [英] Open window and focus textbox tkinter

查看:890
本文介绍了打开窗口和焦点文本框tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(第二个)tkinter窗口,在打开的时候,它并没有得到焦点,而是第一个窗口保持聚焦(尽管第二个窗口出现在另一个窗口的前面)。
它包含一个我希望能够输入的文本框,但是我必须双击才能输入。



如何关注打开窗口时的文本框?

我的尝试:
textbox.focus_set()
window.grab_set()
window.focus_set()

$ b $
$ b

编辑:
相反, .focus_set(b

在关闭 main 窗口时产生错误:无法调用focus命令:application has been destroyed code

这是我的当前代码( tkWin 是主窗口,

  def click(self,field):
import _tkinter
if field!= None:
try:
self.tkcWin = Tk()#创建窗口
self.tkcWin.focus()
self。 tkcWin.title(field)
self.tkcWin.geome try('300x100')
self.mainframe = Frame(master = self.tkcWin,background =#60BF98)
self.mainframe.place(x = 0,y = 0,width = 300 ,height = 300)
self.textb = Text(master = self.mainframe)
self.textb.place(x = 0,y = 50)
self.textb.bind( < Return>,lambda a:self.setM(field))
self.textb.bind(< Return>,lambda a:self.tkcWin.destroy(),True)
self.tkcWin.grab_set()
self.tkWin.wait_window(self.tkcWin)
self.textb.focus_set()
hwnd = self.tkcWin.winfo_id()
ctypes .windll.user32.SetFocus(hwnd)
self.tkcWin.mainloop()
除_tkinter.TclError:
self.tkcWin.destroy()


解决方案

事实证明,您可以简单地调用辅助窗口的 deiconify() 方法,然后控件的 focus_set() 方法:

 toplevel.deiconify()
text.focus_set()



< hr>

以下是Windows的原始解决方法(不再推荐):

首先添加 import ctypes 在顶部。

继续并像以下那样集中你的小部件: text.focus_set()

获取第二个窗口的hwnd: top_hwnd = toplevel.winfo_id()

最后激活第二个窗口: ctypes.windll.user32.SetFocus(top_hwnd)


I have a (second) tkinter window, which, when opened, does not get the focus, but rather the first window remains focused (although the second window appears in front of the other). It contains a textbox which I want to be able to type in, but I have to double-click it in order to type.

How do I focus the textbox when opening the window?

My tries: textbox.focus_set(), window.grab_set(), window.focus_set()

None of them did what I wanted to do.

EDIT: Instead, .focus_set() raises an error when (and only when) closing the main window: can't invoke "focus" command: application has been destroyed

This is my current code (tkWin is the main window, tkcWinis the second window):

def click(self, field):
    import _tkinter
    if field != None:
        try:
            self.tkcWin = Tk()#creating window
            self.tkcWin.focus()
            self.tkcWin.title(field)
            self.tkcWin.geometry('300x100')
            self.mainframe = Frame(master=self.tkcWin,background="#60BF98")
            self.mainframe.place(x=0, y=0, width=300, height=300)
            self.textb = Text(master=self.mainframe)
            self.textb.place(x=0, y=50)
            self.textb.bind("<Return>",lambda a: self.setM(field))
            self.textb.bind("<Return>",lambda a: self.tkcWin.destroy(),True)
            self.tkcWin.grab_set()
            self.tkWin.wait_window(self.tkcWin)
            self.textb.focus_set()
            hwnd = self.tkcWin.winfo_id()
            ctypes.windll.user32.SetFocus(hwnd)
            self.tkcWin.mainloop()
        except _tkinter.TclError:
            self.tkcWin.destroy()

解决方案

It turns out that you can simply call the secondary window's deiconify() method and then the widget's focus_set() method:

toplevel.deiconify()
text.focus_set()


Here's the original work-around for Windows (no longer recommended):

Start by adding import ctypes at the top.
Go ahead and focus your widget like you have with: text.focus_set()
Get the hwnd of the second window: top_hwnd = toplevel.winfo_id()
And finally activate the second window with: ctypes.windll.user32.SetFocus(top_hwnd)

这篇关于打开窗口和焦点文本框tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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