【问题标题】:Python - Gtk - paste from clipboardPython - Gtk - 从剪贴板粘贴
【发布时间】:2016-01-30 07:57:34
【问题描述】:

在 python 和 Glade3 中构建一个简单的 GUI 视频下载器时,我被困在使用图标粘贴到文本输入框中。

应用程序和粘贴图标。

python代码:

# !/usr/python

import sys
import os
import subprocess as sp
try:
    from gi.repository import Gtk, Gdk
except:
    print('Gtk not available')
    sys.exit(1)
try:
    import pyGtk
    pyGtk.require('2.0')
except:
    pass



class VideoDownloader:

  def on_mainwindow_destroy(self, object, data=None):
    print "quit with cancel"
    Gtk.main_quit()

  def __init__(self):
    self.gladefile = "lib/videodownloder.glade" 
    self.builder = Gtk.Builder() 
    self.builder.add_from_file(self.gladefile) 
    self.builder.connect_signals(self)
    self.go = self.builder.get_object
    self.window = self.go("mainwindow")    
    self.window.show() 
    self.okbtn = self.go("okbutton")
    self.cancelbtn = self.go("cancelbutton")
    #self.restartswitch = self.go("restartswitch")
    self.contswitch = self.go("contswitch")
    self.vlcswitch = self.go("vlcswitch")
    self.urlentry = self.go("urlentry")
    self.filechooser = self.go("filechooser")
    self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)

  def on_urlentry_activate(self, widget):
    url = self.urlentry.get_text()
    print("the url is:" + url)

  def on_urlentry_icon_press(self):
    text = self.clipboard.set_text(self.urlentry.get_text(), -1)
    print("the urlentry paste icon was clicked | 'text' ")

  def on_urlentry_icon_press(self):
    text = self.clipboard.wait_for_text()
    print("the urlentry paste icon was clicked | 'text' ")
    if text != None:
      self.urlentry.set_text(text)
      print(" 'text' has been pasted to the urlentry")
    else:
      print("No text on the clipboard")

  def on_filechooser_file_activated(self, widget):
    myfile = self.filechooser.get_uri()
    print("the file is: " + myfile)

  def on_vlcswitch_activate(self, widget):
    print("VLC Switch has been activated")

  def on_contswitch_activate(self, widget):
    print("Continue switch has been acivated")

  def on_quitbutton_clicked(self, button):
    print("quit with the close button")
    Gtk.main_quit()


  def on_okbutton_clicked(self, button):
    myfile = self.filechooser.get_uri()
    url = self.urlentry.get_text()
    wgetcmd = ("wget -O 'myfile ' 'url' ")
    print("ok button has been clicked")
    print("wget will now be run with your args: " +myfile+url)
    os.system(wgetcmd)

if __name__ == "__main__":
  print("videodownloader is running")
  notify = os.system("notify-send --expire-time=5000 --icon='img/vid-down-logo.png' --app-name=VideoDownloader 'VideoDownloader' 'The VideoDownloader app is now running and ready!'")
  notify
  main = VideoDownloader()
  Gtk.main()
  print("videodownloader has stopped running")

当我运行代码时,它大部分都可以工作,但是当我点击粘贴图标时,我得到一个错误:

TypeError: on_urlentry_icon_press() takes exactly 1 argument (4 given)  

我对 python 和 glade 还很陌生,所以我可能犯了一个基本的错误,但我不知道什么是错误的以及错误的含义。我已经搜索过,但只找到了没有帮助的建议。
Such as this.. 请问有什么解决办法吗?

【问题讨论】:

  • 请编辑问题标题 - 它与从剪贴板粘贴无关。
  • 当遇到这样的错误时,定义函数(在这种情况下,on_urlentry_icon_presson_urlentry_icon_press(self, *args),然后检查 args 以查看您到达那里的内容。您甚至可以将定义保留在那里,如果你不需要 args 的任何东西。

标签: python user-interface gtk glade


【解决方案1】:

您当前的on_urlentry_icon_press 方法只接受self 参数,但它应该接受多个参数,因为它连接到icon-press 信号。

documentation可以看到需要:

  • entry(对 Gtk.Entry 的引用)
  • 位置(是主要还是次要图标)
  • 事件(例如是否是双击)

所以它应该是这样的:

def on_urlentry_icon_press(self, entry, position, event):
    print('urlentry icon clicked')

【讨论】:

  • 谢谢@elya5,很棒的信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-23
  • 2015-07-29
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多