Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter在带有文本的对话框中禁用绑定以返回键_Python_Python 3.x_Tkinter - Fatal编程技术网

Python Tkinter在带有文本的对话框中禁用绑定以返回键

Python Tkinter在带有文本的对话框中禁用绑定以返回键,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我的应用程序中有一个简单的对话框,其目的是允许用户编辑一些文本属性。代码的相关部分简化为: 来自tkinter.simpledialog导入对话框的 从tkinter.scrolledtext导入scrolledtext 类属性对话框(对话框): 定义初始化(自,根): 对话框。初始化(self,root) def主体(自身、主): 标签(master,text=“Description”).grid(行=0,列=0,粘性=W) self.project_description=滚动文本(主控,

我的应用程序中有一个简单的对话框,其目的是允许用户编辑一些文本属性。代码的相关部分简化为:

来自tkinter.simpledialog导入对话框的

从tkinter.scrolledtext导入scrolledtext
类属性对话框(对话框):
定义初始化(自,根):
对话框。初始化(self,root)
def主体(自身、主):
标签(master,text=“Description”).grid(行=0,列=0,粘性=W)
self.project_description=滚动文本(主控,高度=10,宽度=40)
self.project\u description.grid(行=0,列=1,粘滞=W)
不幸的是,在
滚动文本
字段中键入时使用
键不会被对话框解释为换行符,而是被解释为“确定”,因此对话框关闭。我认为将该键绑定到一个不起任何作用的处理程序会有所帮助,但是在对话框的
主体
方法的开头插入
self.bind(“,self.my\u return\u handler)
不会截取任何内容,并在
对话框之后插入它。
引发以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "guitk.py", line 142, in edit_project_properties
    ProjectPropertiesDialog(self.main_window, None)
  File "guitk.py", line 337, in __init__
    self.bind("<Return>", self.return_handler)
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1251, in bind
    return self._bind(('bind', self._w), sequence, func, add)
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1206, in _bind
    self.tk.call(what + (sequence, cmd))
_tkinter.TclError: bad window path name ".!projectpropertiesdialog"
Tkinter回调中出现异常 回溯(最近一次呼叫最后一次): 文件“/usr/lib/python3.7/tkinter/_init__.py”,第1705行,在调用中__ 返回self.func(*args) 编辑项目属性中的文件“guitk.py”,第142行 项目属性对话框(self.main_窗口,无) 文件“guitk.py”,第337行,在__ self.bind(“,self.return\u处理程序) 文件“/usr/lib/python3.7/tkinter/_init_uuu.py”,第1251行,在bind中 返回self.\u bind(('bind',self.\u w),sequence,func,add) 文件“/usr/lib/python3.7/tkinter/_init__.py”,第1206行,在绑定中 self.tk.call(what+(sequence,cmd)) _tkinter.TclError:错误的窗口路径名“!projectpropertiesdialog”
我该如何解决这个问题?有更合适的方法吗?

如果您使用

import tkinter.simpledialog 
print(tkinter.simpledialog.__file__)
然后获得源代码的路径,您将看到它绑定到哪里,然后可以删除它

我从源代码中复制了原始的
buttonbox()
,并注释了绑定


从tkinter导入*
从tkinter.simpledialog导入对话框
从tkinter.scrolledtext导入scrolledtext
类属性对话框(对话框):
def主体(自身、主):
标签(master,text=“Description”).grid(行=0,列=0,粘性=W)
self.project_description=滚动文本(主控,高度=10,宽度=40)
self.project\u description.grid(行=0,列=1,粘滞=W)
self.project_description.focus()
def按钮盒(自身):
super().buttonbox()
自动解除绑定(“”)
#self.bind(“,self.ok)#将其他组合绑定到ok
root=Tk()
属性对话框(根)
root.mainloop()

try:widget.bind(“”,lambda e:“break”)解除绑定此键的默认绑定。您可以检查对话框源代码,查看哪个小部件绑定了Return并解除绑定。请参见导入tkinter.simpledialog
打印(tkinter.simpledialog.\uuu文件)
。或者使用
Toplevel()
窗口从头开始创建自己的对话框。是的,我错误地认为一切都是在
\uuuu init\uuuu
中设置的。谢谢,这完全符合预期。我最终将
buttonbox
改写为
Dialog.buttonbox(self);self.unbind(“”)
。同样的效果,对我来说更干净。你是对的。这要简单得多。我把它添加到答案中——它对其他用户很有用。
from tkinter import *
from tkinter.simpledialog import Dialog
from tkinter.scrolledtext import ScrolledText

class PropertiesDialog(Dialog):

    def body(self, master):
        Label(master, text="Description").grid(row=0, column=0, sticky=W)
        self.project_description = ScrolledText(master, height=10, width=40)
        self.project_description.grid(row=0, column=1, sticky=W)
        self.project_description.focus()

    def buttonbox(self):
        '''add standard button box.

        override if you do not want the standard buttons
        '''

        box = Frame(self)

        w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)
        w.pack(side=LEFT, padx=5, pady=5)
        w = Button(box, text="Cancel", width=10, command=self.cancel)
        w.pack(side=LEFT, padx=5, pady=5)

        #self.bind("<Return>", self.ok)  # <-- remove it
        self.bind("<Escape>", self.cancel)

        box.pack()

root = Tk()
PropertiesDialog(root)
root.mainloop()
def buttonbox(self):
    super().buttonbox()
    self.unbind("<Return>")
    #self.bind("<Control-Return>", self.ok)  # bind other combination to OK
from tkinter import *
from tkinter.simpledialog import Dialog
from tkinter.scrolledtext import ScrolledText

class PropertiesDialog(Dialog):

    def body(self, master):
        Label(master, text="Description").grid(row=0, column=0, sticky=W)
        self.project_description = ScrolledText(master, height=10, width=40)
        self.project_description.grid(row=0, column=1, sticky=W)
        self.project_description.focus()

    def buttonbox(self):
        super().buttonbox()
        self.unbind("<Return>")
        #self.bind("<Control-Return>", self.ok)  # bind other combination to OK

root = Tk()
PropertiesDialog(root)
root.mainloop()