【问题标题】:python tkinter - How do I get get menu "select all" function to work?python tkinter - 如何让菜单“全选”功能工作?
【发布时间】:2019-11-21 22:22:32
【问题描述】:

我正在尝试向该程序添加一个带有“全选”的菜单。我希望它选择答案文本框中的所有文本,即“blank2”。我可以使菜单和下拉菜单正常工作,但是当我单击“全选”时,它给了我一个错误。 这是代码...

#!/usr/bin/env python3
from tkinter import *

#function for select all menu item
def select_all(event=None):
    blank2.tag_add('sel', '1.0', 'end')
    return "break"

def getDaText():
    src = blank.get()

    if '_' in src:
        dst = src.replace("_", " ")
    else:
        dst = src.replace(" ", "_")

    blank2.insert(0, dst)

def clear_answer():

    blank.delete('0', END)
    blank2.delete('0', END)

main = Tk()
# Creating Menubar
menubar = Menu(main)

# Adding Edit Menu and commands
edit = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Edit', menu = edit)
#edit.add_command(label ='Cut', command = None)
#edit.add_command(label ='Copy', command = None)
#edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command=select_all)

Label(main, text=" add or remove underscore ").grid(row=0, columnspan=3, sticky=W, pady=1)

blank = Entry(main)
blank2 = Entry(main)

blank.grid(row=1, column=0, columnspan=2, sticky=W)
blank2.grid(row=2, column=0, columnspan=2, sticky=W)

Button(main, text='Show answer', command=getDaText).grid(row=3, column=0, sticky=W, pady=1)
Button(main, text='Clear', command=clear_answer).grid(row=3, column=1, sticky=W, pady=1)

# display Menu
main.config(menu = menubar)
mainloop()

这是错误...

Exception in Tkinter callback

Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
    return self.func(*args)
  File "/home/mthomas/python_progs/add_or_remove_underscore/add_or_remove_underscore-tk-with-label.py", line 5, in select_all
    blank2.tag_add('sel', '1.0', 'end')
AttributeError: 'Entry' object has no attribute 'tag_add'

【问题讨论】:

  • 你缺少函数tag_add

标签: python python-3.x tkinter


【解决方案1】:

您在Entry-Widget 上调用方法tag_add。这个小部件没有tag_add 方法。可能您想改用Text-Widget。

# replace
blank2 = Entry(main)
# with
blank2 = Text(main)

参考:

入口小部件:https://www.tutorialspoint.com/python/tk_entry.htm

文本小部件:https://www.tutorialspoint.com/python/tk_text.htm

编辑

我已经使用Entry-Widget 寻找解决方案并找到以下内容:

def select_all(event=None)
    # first set the focus to the entry widget
    blank2.focus()

    # use the selection_range method from the entry widget
    blank2.selection_range(0, END)

https://effbot.org/tkinterbook/entry.htm#Tkinter.Entry.selection_range-method

Setting focus to specific TKinter entry widget

【讨论】:

  • 不,当单击“显示答案”按钮时导致主程序失败 - “返回 self.func(*args) 文件”/home/mthomas/python_progs/add_or_remove_underscore/add_or_remove_underscore-tk-with- label.py”,第 16 行,getDaText blank2.insert(0, dst) 文件“/usr/lib/python3.5/tkinter/__init__.py”,第 3121 行,插入 self.tk.call((self. _w, 'insert', index, chars) + args) _tkinter.TclError: bad text index "0" " GUI 也关闭了,答案字段要大得多。
  • 谢谢,成功了!想给你点赞,但是我的用户等级太低了。
【解决方案2】:

这也应该有效。第一:pip install pyautogui

import pyautogui
def select_all():
    pyautogui.hotkey("ctrl", "a") #Fire the CTRL + A event

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 2019-12-12
    • 2021-04-06
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多