PyQt4 有个问题问问大家。。

2017-01-12 23:57:47 +08:00
 tumbzzc

-- 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()函数。。

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

1671 次点击
所在节点    问与答
5 条回复
tumbzzc
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
2017-01-13 00:04:46 +08:00
帮顶
tumbzzc
2017-01-13 00:06:52 +08:00
@idea4j thanks ,看来你也在学?
idea4j
2017-01-13 00:11:33 +08:00
@tumbzzc 主要学 java 现在~看到楼主是做百度图床,也有这样的想法,就帮顶啦
idea4j
2017-01-13 00:14:32 +08:00
@tumbzzc /t/334253 最近想鼓捣个这个(有情顶一下最好了 = =

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/334251

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX