pyperclip 的奇怪行为
Weird behavior of pyperclip
我正在尝试从 Tkinter 中的 Text 小部件复制所选单词。使用 pyperclip.copy(text)
复制文本效果很好,但是当我尝试使用 pyperclip.paste()
粘贴复制的文本时却得到了 ᥈H֛
作为输出。我不知道这是怎么发生的,是什么原因导致的。
系统:Windows10
Python版本:3.7.8
代码段
def get_selected_text(self):
'''Return the selected text if selection is available'''
try:
return self.text_widget.get('sel.first', 'sel.last').strip().strip('\n')
except TclError:
if 'found' in self.text_widget.tag_names():
return self.text_widget.get('found.first', 'found.last').strip().strip('\n')
def copy(self, event=None):
'''Copy functionality when user clicks cut option or presses Ctrl+C'''
text = self.get_selected_text()
pyperclip.copy(text)
def paste(self, event=None):
'''Cut functionality when user clicks cut option or presses Ctrl+X'''
cursor_pos = self.text_widget.index('insert')
print(pyperclip.paste()) # Getting weird value '䯀͏H' but when called inside copy function then I get the exact value but not in this function
self.text_widget.insert(cursor_pos, pyperclip.paste())
self.text_widget.see(cursor_pos)
return 'break'
我做错了什么?
这不是pyperclip的问题。真正的问题在于我的代码本身。所以,问题是我绑定了 ctrl+c
来复制和绑定 ctrl+X
来剪切文本。
According to this 如果在函数中找不到 return break
那么小部件的默认绑定和自定义绑定将被执行,这意味着当我按下 'Ctrl+X
然后 paste
函数在默认绑定和当文本小部件本身默认绑定时 奇怪的文本被打印 could not figure why this happens
但是在 copy, cut and paste
函数末尾添加 return break
解决了我的问题。
我正在尝试从 Tkinter 中的 Text 小部件复制所选单词。使用 pyperclip.copy(text)
复制文本效果很好,但是当我尝试使用 pyperclip.paste()
粘贴复制的文本时却得到了 ᥈H֛
作为输出。我不知道这是怎么发生的,是什么原因导致的。
系统:Windows10
Python版本:3.7.8
代码段
def get_selected_text(self):
'''Return the selected text if selection is available'''
try:
return self.text_widget.get('sel.first', 'sel.last').strip().strip('\n')
except TclError:
if 'found' in self.text_widget.tag_names():
return self.text_widget.get('found.first', 'found.last').strip().strip('\n')
def copy(self, event=None):
'''Copy functionality when user clicks cut option or presses Ctrl+C'''
text = self.get_selected_text()
pyperclip.copy(text)
def paste(self, event=None):
'''Cut functionality when user clicks cut option or presses Ctrl+X'''
cursor_pos = self.text_widget.index('insert')
print(pyperclip.paste()) # Getting weird value '䯀͏H' but when called inside copy function then I get the exact value but not in this function
self.text_widget.insert(cursor_pos, pyperclip.paste())
self.text_widget.see(cursor_pos)
return 'break'
我做错了什么?
这不是pyperclip的问题。真正的问题在于我的代码本身。所以,问题是我绑定了 ctrl+c
来复制和绑定 ctrl+X
来剪切文本。
According to this 如果在函数中找不到 return break
那么小部件的默认绑定和自定义绑定将被执行,这意味着当我按下 'Ctrl+X
然后 paste
函数在默认绑定和当文本小部件本身默认绑定时 奇怪的文本被打印 could not figure why this happens
但是在 copy, cut and paste
函数末尾添加 return break
解决了我的问题。