V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
tumbzzc
V2EX  ›  问与答

PyQt4 有个问题问问大家。。

  •  
  •   tumbzzc · 2017-01-12 23:57:47 +08:00 · 1549 次点击
    这是一个创建于 2672 天前的主题,其中的信息可能已经有所发展或是发生改变。

    -- coding=utf8 --

    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    from PyQt4 import QtCore, QtGui, uic
    import requests
    import re
    from PyQt4.QtCore import * 
    from PyQt4.QtGui import *
    
    
    tc_api='http://image.baidu.com/pictureup/uploadshitu'
    files={
        'image':''
        }
    data={'pos':'upload'
        ,'uptype':'upload_pc'
        ,'fm':'index'}
    
    def upload_file(filepath):
        try:
            img=open(filepath,'rb')
        except Exception,e:
            print e
            sys.exit(0)
        files['image']=img
        c=requests.post(tc_api,files=files,data=data)
        img_url=re.findall('queryImageUrl=(.*?)&querySign',c.url)[0]
        img1=re.sub('%3A',':',img_url)
        img2=re.sub('%2F','/',img1)
        return img2
    
    qtCreatorFile = "baidu_ui.ui" # Enter file here.
    
    Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
    
    class MyApp(QtGui.QMainWindow, Ui_MainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)
            Ui_MainWindow.__init__(self)
            self.setupUi(self)
            self.thread=Worker()
            self.fileSelect.clicked.connect(self.selectFile)
            self.thread.sinOut2.connect(self.show_result)
    
        def selectFile(self):
            filepath=self.to_utf8(QtGui.QFileDialog.getOpenFileName(self,u'选择图片','',r'Image Files(*.png *.jpg *.bmp *.jpeg *.gif)'))
            self.thread.getPath((filepath,))
            self.fileSelect.setEnabled(False)
    
    
        def show_result(self,result):
            img,isTrue=result[0],result[1]
            self.markdown_show.setText('![]('+img+')')
            self.realurl.setText(img)
            self.fileSelect.setEnabled(isTrue)
    
        def to_utf8(self,input):
            return unicode(input,'utf8','ignore')
    
    
    class Worker(QtCore.QThread): 
        sinOut2 = pyqtSignal(tuple) 
    
        def __init__(self,parent=None): 
            super(Worker,self).__init__(parent) 
    
        def getPath(self,filepath):
            self.filepath=filepath[0]
            print self.filepath
    
        def run(self): 
            while 1:
                print 'start'
                img=upload_file(self.filepath)
                print img
                self.sinOut2.emit((img,True))
    
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        window = MyApp()
        window.show()
        sys.exit(app.exec_())
    

    本质是用 QT 写个窗口,然后选择图片,然后上传至百度识图,返回图片链接,然后把图片链接放在 QLineEdit 。

    现在问题是: MyApp 可以将 filepath 传到 worker ,但是 worker 好像没有执行 run(),也就没有执行 upload_file()函数。。

    没看出什么问题(修为不够),求各位大神看看

    5 条回复    2017-01-13 00:14:32 +08:00
    tumbzzc
        1
    tumbzzc  
    OP
       2017-01-12 23:58:37 +08:00
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>Form</class>
    <widget class="QWidget" name="Form">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>395</width>
    <height>154</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>百度图床</string>
    </property>
    <widget class="QLineEdit" name="markdown_show">
    <property name="geometry">
    <rect>
    <x>120</x>
    <y>20</y>
    <width>251</width>
    <height>31</height>
    </rect>
    </property>
    </widget>
    <widget class="QLineEdit" name="realurl">
    <property name="geometry">
    <rect>
    <x>120</x>
    <y>60</y>
    <width>251</width>
    <height>31</height>
    </rect>
    </property>
    </widget>
    <widget class="QLabel" name="label_markdown">
    <property name="geometry">
    <rect>
    <x>30</x>
    <y>20</y>
    <width>91</width>
    <height>21</height>
    </rect>
    </property>
    <property name="font">
    <font>
    <family>微软雅黑</family>
    <weight>75</weight>
    <bold>true</bold>
    </font>
    </property>
    <property name="text">
    <string>MarkDown</string>
    </property>
    <property name="textFormat">
    <enum>Qt::RichText</enum>
    </property>
    </widget>
    <widget class="QLabel" name="label_markdown_2">
    <property name="geometry">
    <rect>
    <x>50</x>
    <y>60</y>
    <width>61</width>
    <height>21</height>
    </rect>
    </property>
    <property name="font">
    <font>
    <family>微软雅黑</family>
    <weight>75</weight>
    <bold>true</bold>
    </font>
    </property>
    <property name="text">
    <string>图片链接</string>
    </property>
    <property name="textFormat">
    <enum>Qt::RichText</enum>
    </property>
    </widget>
    <widget class="QPushButton" name="fileSelect">
    <property name="geometry">
    <rect>
    <x>120</x>
    <y>110</y>
    <width>93</width>
    <height>28</height>
    </rect>
    </property>
    <property name="text">
    <string>选择图片</string>
    </property>
    </widget>
    </widget>
    <resources/>
    <connections/>
    </ui>


    这是 baidu_ui.ui
    idea4j
        2
    idea4j  
       2017-01-13 00:04:46 +08:00
    帮顶
    tumbzzc
        3
    tumbzzc  
    OP
       2017-01-13 00:06:52 +08:00 via Android
    @idea4j thanks ,看来你也在学?
    idea4j
        4
    idea4j  
       2017-01-13 00:11:33 +08:00
    @tumbzzc 主要学 java 现在~看到楼主是做百度图床,也有这样的想法,就帮顶啦
    idea4j
        5
    idea4j  
       2017-01-13 00:14:32 +08:00
    @tumbzzc /t/334253 最近想鼓捣个这个(有情顶一下最好了 = =
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3468 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:13 · PVG 20:13 · LAX 05:13 · JFK 08:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.