gpt4 book ai didi

python - Python 中的 focus_set() 在第二个窗口上不起作用

转载 作者:行者123 更新时间:2023-12-05 07:57:04 25 4
gpt4 key购买 nike

getinput 窗口出现在介绍窗口之后,应该聚焦于输入空间,但实际上没有。如果我跳过介绍窗口,则会发生焦点。如果我随后输入 1-10 范围之外的数字,getinput 窗口会重新出现,但没有焦点。有人可以帮忙吗?

用Python 3.3.5编写

import tkinter

def intro(tkinter):
global contents
w = 300
h = 200
frame = tkinter.Frame(root,width=w,height=h)
frame.pack()
label = tkinter.Label(frame,font=('courier', 12, 'bold'),\
text="\nPlease click on OK.\n\n")
label.pack()
label.place(x=40,y=50)
OK = tkinter.Button(root,bd=5,text='OK',font=('courier', 20, 'bold'),\
command=root.destroy)
OK.pack()
OK.place(x=125,y=120)
root.mainloop()

def cancel():
global level
level = contents.get()
root.destroy()

def getinput(tkinter):
global contents
w = 400
h = 300
frame = tkinter.Frame(root,width=w,height=h)
frame.pack()
label = tkinter.Label(frame,font=('courier', 12, 'bold'),\
text="Please enter a number in the\n range 1 to 10 and click on OK.")
label.pack()
label.place(x=40,y=50)
entry = tkinter.Entry(frame,font=('courier', 24, 'bold'))
entry.pack()
entry.place(x=175,y=150,width=50)
contents = tkinter.StringVar()
contents.set("")
entry["textvariable"] = contents
entry.focus_set() #Gives focus by flashing cursor in entry space.
OK = tkinter.Button(root,bd=5,text='OK',font=('courier', 20, 'bold'),\
command=cancel)
OK.pack()
OK.place(x=170,y=210)
root.mainloop()

level = ""
contents = ""
root = tkinter.Tk() #Comment out this line
intro(tkinter) #and this line to skip intro window
root = tkinter.Tk()
getinput(tkinter)
while (int(level) < 1) or (int(level) > 10):
root = tkinter.Tk()
getinput(tkinter)

print('Result is ',level)

最佳答案

我遇到了同样的问题。感谢@fhdrsdg,entry.focus_force() 搞清楚了。下面是一个例子。

from tkinter import *
root = Tk()
Window = Frame(root)
Window.pack()
txt = Entry(Window)
txt.pack()
txt.focus_set()
root.mainloop()

root = Tk()
Window = Frame(root)
Window.pack()
txt = Entry(Window)
txt.pack()
txt.focus_force()
root.mainloop()

关于python - Python 中的 focus_set() 在第二个窗口上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28011107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com