您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Python集成C#實(shí)現(xiàn)界面操作下載文件功能的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
我很熟悉用 C# & WinForm 的方式開(kāi)發(fā)界面,現(xiàn)在剛好學(xué)習(xí)了 Python 的網(wǎng)絡(luò)編程的基礎(chǔ)庫(kù) socket,于是我就想到寫(xiě)一個(gè)程序,思路如下:
程序運(yùn)行時(shí)會(huì)打開(kāi)一個(gè) WinForm 窗體,窗體上有:
輸入文件下載地址的地址欄
選擇文件保存位置的文件開(kāi)窗按鈕
當(dāng)前下載狀態(tài)的狀態(tài)區(qū)域
下載按鈕
輸入下載地址,選擇一個(gè)文件保存位置
點(diǎn)擊下載按鈕下載文件,狀態(tài)區(qū)域顯示文件下載狀態(tài),最好能顯示下載進(jìn)度
界面放到 WinForm,下載功能放到 Python
WinForm 分為幾部分功能
界面設(shè)計(jì)
提供下載地址的公共屬性
提供文件存儲(chǔ)地址公共屬性
提供用于委托下載事件的委托定義
提供記錄狀態(tài)信息的公共方法
提供更新進(jìn)度信息的公共方法
首先我們使用 VS 創(chuàng)建一個(gè)類庫(kù)項(xiàng)目
至于為什么沒(méi)有使用 .NET 5 或者 .net core,是因?yàn)椋篜ython 調(diào)用 C# 動(dòng)態(tài)鏈接庫(kù)
創(chuàng)建項(xiàng)目后新建窗體
本例中設(shè)計(jì)界面設(shè)計(jì)如下:
/// <summary> /// 當(dāng)前地址 /// </summary> public string ThisUrl { get { return textUrl.Text; } } /// <summary> /// 當(dāng)前保存路徑 /// </summary> public string ThisSavePath { get { return textSavePath.Text; } }
/// <summary> /// 下載事件委托 /// </summary> public event EventHandler DownloadEvent; /// <summary> /// 下載按鈕事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonDownload_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.textUrl.Text)) { MessageBox.Show("請(qǐng)先輸入要下載的文件地址!"); this.textUrl.Focus(); return; } if (string.IsNullOrEmpty(this.textSavePath.Text)) { MessageBox.Show("請(qǐng)先選擇文件要保存的地址!"); this.textSavePath.Focus(); return; } // 調(diào)用委托事件 if(this.DownloadEvent != null) { this.DownloadEvent.Invoke(this, e); } }
打開(kāi)選擇保存文件路徑時(shí)候由于會(huì)報(bào)錯(cuò)
在可以調(diào)用OLE之前,必須將當(dāng)前線程設(shè)置為單線程單元(STA)模式,請(qǐng)確保您的Main函數(shù)帶有STAThreadAttribute標(biāo)記
很無(wú)奈,因?yàn)槲覀兊恼{(diào)用方并不是 C# 的 Main 函數(shù),而我目前并不知道 Python 調(diào)用 C# 如何實(shí)現(xiàn)的,所以只能另外想方法,就是把選擇保存文件路徑的開(kāi)窗單獨(dú)啟一個(gè)線程開(kāi)發(fā),在子線程上再標(biāo)記 STA
/// 選擇按鈕事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonSelect_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.textUrl.Text)) { MessageBox.Show("請(qǐng)先輸入要下載的文件地址!"); this.textUrl.Focus(); return; } var index = this.textUrl.Text.LastIndexOf("/"); var fileName = this.textUrl.Text.Substring(index + 1); Thread importThread = new Thread(() => { var text = OpenDialog(fileName); MessageEvent(text); }); importThread.SetApartmentState(ApartmentState.STA); //重點(diǎn) importThread.Start(); } /// <summary> /// 打開(kāi)對(duì)話框 /// </summary> private string OpenDialog(string fileName) { var saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "所有文件 (*.*)|*.*"; saveFileDialog.FilterIndex = 0; if (!string.IsNullOrEmpty(fileName)) { saveFileDialog.FileName = Path.Combine(saveFileDialog.FileName, fileName); } DialogResult dialogResult = saveFileDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { return saveFileDialog.FileName; } return String.Empty; }
Python 中分幾部分功能
程序調(diào)用 .NET 類庫(kù)打開(kāi)窗體
程序中存在下載指定 URL 文件存儲(chǔ)到指定路徑的函數(shù)定義
程序結(jié)束的函數(shù)定義
把當(dāng)前程序封裝成可運(yùn)行程序(如:Windows 中為封裝成 exe)
import socket import time import re mainapp = None # 調(diào)用動(dòng)態(tài)鏈接庫(kù)的更新?tīng)顟B(tài)信息 def LogInfo(text): # print(text) mainapp.LogInfo(text) # 調(diào)用動(dòng)態(tài)鏈接庫(kù)的更新下載進(jìn)度 def downloadInfo(c, all): mainapp.SetProcess(c, all) if c == all: # LogInfo("下載進(jìn)度 {:.2f}".format(c / all * 100)) LogInfo("下載完成。") # else: # LogInfo("下載進(jìn)度 {:.2f}%".format(c / all * 100)) # 監(jiān)聽(tīng)下載委托事件 def Download(source, args): thisurl = source.ThisUrl.lower() thispath = source.ThisSavePath LogInfo("下載地址是: {}".format(thisurl)) LogInfo("保存路徑為: {}".format(thispath)) reobj = re.compile(r"""(?xi)\A [a-z][a-z0-9+\-.]*:// # Scheme ([a-z0-9\-._~%!$&'()*+,;=]+@)? # User ([a-z0-9\-._~%]+ # Named or IPv4 host |\[[a-z0-9\-._~%!$&'()*+,;=:]+\]) # IPv6+ host """) match = reobj.search(thisurl) if match: HOST = match.group(2) PORT = 443 if thisurl.startswith('https') else 80 mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect((HOST, PORT)) urlend = 'GET {} HTTP/1.0\r\n\r\n'.format(thisurl).encode() # LogInfo("傳遞參數(shù): {}".format(urlend)) LogInfo("開(kāi)始下載……") mysock.sendall(urlend) count = 0 picture = b"" hearlength = 0 filelength = 0 hearc = b"" while True: data = mysock.recv(5120) if (len(data) < 1): break time.sleep(0.1) count = count + len(data) picture = picture + data # print(len(data), count) if hearlength == 0: hearlength = picture.find(b"\r\n\r\n") if hearlength > 0: hearc = picture[:hearlength].decode() # print(hearc) sear = re.search('Content-Length: ([0-9]+)', hearc) if sear: filelength = int(sear.groups()[0]) downloadInfo(count - 4 - hearlength, filelength) else: downloadInfo(count - 4 - hearlength, filelength) mysock.close() # Skip past the header and save the picture data picture = picture[hearlength+4:] fhand = open(thispath, "wb") fhand.write(picture) fhand.close() # Code: http://www.py4e.com/code3/urljpeg.py # Or select Download from this trinket's left-hand menu else: LogInfo('下載失敗,地址格式存在問(wèn)題!') # 使用 pythonnet 的方式引入動(dòng)態(tài)鏈接庫(kù) import clr # 此處保證動(dòng)態(tài)鏈接庫(kù)文件放在當(dāng)前文件夾中,如果不在應(yīng)該使用這種方式 # clr.AddReference('D:\\Path\\DotNetWithPython') # clr.AddReference('D:\\Path\\DotNetWithPython.dll') clr.AddReference('DotNetWithPython') from DotNetWithPython import * mainapp = MainForm() mainapp.DownloadEvent += Download mainapp.ShowDialog()
功能實(shí)現(xiàn)了,但是存在一個(gè)無(wú)法解決的問(wèn)題,就是當(dāng)文件開(kāi)始下載后 WinForm 的界面會(huì)卡住,疑似是沒(méi)有用現(xiàn)線程打開(kāi)主窗體的原因,但是不能解釋為什么下載開(kāi)始的時(shí)候沒(méi)有卡頓,有哪位大白指導(dǎo)一下呢?不勝感激!
感謝各位的閱讀!關(guān)于“Python集成C#實(shí)現(xiàn)界面操作下載文件功能的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。