Tkinter 剪贴板错误 [英] Tkinter clipboard errors

查看:49
本文介绍了Tkinter 剪贴板错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想要做的是将我复制的任何内容更改为我复制的内容,但都是大写的.每当我尝试运行以下程序时,它都会崩溃.

Basically what I'm trying to do is change whatever I copy into what I copied but it's all uppercase. Whenever I try to run the following program it crashes.

from tkinter import *

root = Tk()

prev_clip = root.clipboard_get()

while True:
    prev_clip = root.clipboard_get()
    root.clipboard_append(prev_clip.upper())

root.mainloop()

如果用这种方式做我想做的事情是不可能的,那么告诉我我该怎么做.不过,如果我能这样做,那就更好了.

If it's impossible to do what I want this way, then tell me how I can do it. It would be preferred if I could do it this way, though.

这是错误:

Traceback (most recent call last):
  File "[file location of the program]",     line 5, in <module>
    prev_clip = root.clipboard_get()
  File     "[file location of program]",     line 652, in clipboard_get
    return self.tk.call(('clipboard', 'get') + self._options(kw))
_tkinter.TclError: CLIPBOARD selection doesn't exist or form "STRING" not defined

推荐答案

在做了比我的初始实验更多的事情后我不确定您要做什么,但错误正是消息所说的:剪贴板中没有任何内容,甚至没有空字符串.为防止出现这种情况,请使用 try-except 块或使用 root.clipboard_append('') 附加一个空字符串(参见第二个代码示例).

EDITED AFTER DOING MORE THAN MY INITIAL EXPERIMENTS: I am not sure what you are trying to do, but the error is just what the message says: there is nothing in the clipboard, not even an empty string. To prevent this, either use a try-except block or append a empty string with root.clipboard_append('') (see second code example).

如果此问题已修复,您的无限循环 (while True:) 将在每次循环中将剪贴板文本的长度加倍,直到出现不同的崩溃.

If this were fixed, your infinite loop (while True:) would double the length of the clipboard text each loop until you got a different crash.

from tkinter import *
r = Tk()
r.clipboard_clear()
r.clipboard_append('abc ')
for i in range(5):
    clip = r.clipboard_get()
    print(clip)
    r.clipboard_append(clip.upper())

印刷品

abc 
abc ABC 
abc ABC ABC ABC 
abc ABC ABC ABC ABC ABC ABC ABC 
abc ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC 

这肯定不是你想要的.

也许您想要的是由用户在剪贴板中输入新选择触发的功能.该函数将在附加新版本之前清除剪贴板(如 T. Kropalis 所建议的).不幸的是,据我所知,没有可通过 tkiter 访问的 CLIPBOARD-CHANGED 事件.

Perhaps what you want is a function triggered by the user entering a new selection into the clipboard. The function would clear the clipboard before appending the new version (as suggested by T. Kropalis). Unfortunately, there is not, as far as I know, a CLIPBOARD-CHANGED event accessible via tkiter.

我的一个想法是将函数绑定到 ^X 和 ^C,以便在剪切或复制操作之后调用该函数.我尝试过但失败了,我不确定这是否可行.

One thought I had was to bind the function to ^X and ^C in such a way that the function would be called after the Cut or Copy operation. I tried and failed and am not sure it is possible.

另一个是选择其他键来表示'复制和大写'和'剪切和大写'.然后该函数必须触发剪切或复制操作以在大写之前更改剪贴板,或者直接剪切或复制当前选择.我相信后者是可以做到的.

Another is to choose other keys to mean 'Copy and uppercase' and 'Cut and uppercase'. The function would then have to trigger the Cut or Copy operation to change the clipboard before uppercasing it, or directly Cut or Copy the current selection. I believe the latter could be done.

第三个选项是后台循环来修改剪贴板.

A third option is a background loop to modify the clipboard.

from tkinter import *
root = Tk()

delay = 30  # milliseconds

def upper():
    clip = root.clipboard_get()
    root.clipboard_clear()
    root.clipboard_append(clip.upper())
    root.after(delay, upper)

tex = Text(root)
tex.pack()
tex.insert('insert', 'Some starting text\n')

root.clipboard_append('')  # Avoid "CLIPBOARD selection doesn't exist" error.
root.after(delay, upper)
root.mainloop()

upper 的改进版本可能会将当前内容与先前内容(大写后)的保存副本进行比较,并且仅在内容更改时才更改剪贴板.

An improved version of upper might compare the current contents to the a saved copy of the previous contents (after uppercased) and only change the clipboard if the contents changed.

这篇关于Tkinter 剪贴板错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆