首页 / PYTHON / Python获得专注的条目名称
Python获得专注的条目名称
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python获得专注的条目名称,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2127字,纯文字阅读大概需要4分钟。
内容图文
![Python获得专注的条目名称](/upload/InfoBanner/zyjiaocheng/809/fa1fca665979429e9c802eccb53ea648.jpg)
每当按下向上或向下箭头键时,我都试图使输入值增加或减少.要做到这一点,我需要首先找到哪个条目是焦点,我正在尝试这样做“.focus_get()”.问题是我无法弄清楚它是如何工作的或它的返回方式.它为每个条目返回1个唯一编号,类似于:“.45191744”,但每次运行程序时此编号都会更改.
运行代码时,以下数字表示最近5次尝试.
” 0.50518728\”
” 0.53009096\”
” 0.55889592\”
” 0.51891896\”
如何获得焦点条目的变量名称?
这是我的代码:
def get_focus1(event):
print("return: event.widget is", event.widget)
print("focus is:", window2.focus_get())
print(window2.focus_get())
print(help(window2.Entry))
window2 = Tk()
eyear1 = Entry(window2, width=4, font=("Helvetica", 16)) # Entry for year
eyear1.insert(10, defaultYear)
eyear1.grid(row=1, column=1)
emonth1 = Entry(window2, width=4, font=("Helvetica", 16)) # Entry for Month
emonth1.insert(10, defaultMonth)
emonth1.grid(row=1, column=2)
eday1 = Entry(window2, width=4, font=("Helvetica", 16)) # Entry for day
eday1.insert(10, defaultDay)
eday1.grid(row=1, column=3)
window2.bind('<Left>', get_focus1)
mainloop()
解决方法:
focus_get返回实际对象.你想做什么,假设你没有充分使用textvariable(参见Bryan的评论),就是清除文本并重新写入新值(显然要做一些验证).你最终得到的是这样的:
from tkinter import *
def up(event):
# warning, exceptions can happen
old = int(event.widget.get()) # this gives back the actual object!
event.widget.delete(0, END) # delete existing text
event.widget.insert(10, old + 1) # put new text in
def down(event):
# warning, exceptions can happen
old = int(event.widget.get()) # this gives back the actual object!
event.widget.delete(0, END) # delete existing text
event.widget.insert(10, old - 1) # put new text in
window2 = Tk()
eyear1 = Entry(window2, width=4, font=("Helvetica", 16)) # Entry for year
eyear1.insert(10, 2015)
eyear1.grid(row=1, column=1)
emonth1 = Entry(window2, width=4, font=("Helvetica", 16)) # Entry for Month
emonth1.insert(10, 1)
emonth1.grid(row=1, column=2)
eday1 = Entry(window2, width=4, font=("Helvetica", 16)) # Entry for day
eday1.insert(10, 10)
eday1.grid(row=1, column=3)
# bind both keys to corresponding event handlers
window2.bind('<Up>', up)
window2.bind('<Down>', down)
mainloop()
内容总结
以上是互联网集市为您收集整理的Python获得专注的条目名称全部内容,希望文章能够帮你解决Python获得专注的条目名称所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。