Python中怎么自定義對話框

Python中怎么自定義對話框,很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),莆田企業(yè)網(wǎng)站建設(shè),莆田品牌網(wǎng)站建設(shè),網(wǎng)站定制,莆田網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,莆田網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

首先在彈出菜單中增加修改文件名菜單項(xiàng):

def rbutton_down(event):    iid = list_view.identify_row(event.y)    if iid:        if iid not in list_view.selection():            list_view.selection_set(iid)            list_view.focus(iid)        path, selections = selected_files()        if path:            menu = Menu(list_view, tearoff=False)            menu.add_command(label='Open', command=open_current)            if len(list(selections))==1:                menu.add_command(label='Rename', command=rename_current)            menu.add_command(label='Delete', command=delete_current)            menu.post(event.x_root, event.y_root)

代碼11行首先判斷選中的文件個(gè)數(shù),如果為1則增加【Rename】菜單項(xiàng)。當(dāng)用戶選擇【Rename】時(shí),系統(tǒng)會(huì)調(diào)用rename_current函數(shù),其實(shí)現(xiàn)如下:

def rename_current():    path, selections = selected_files()    if path:        for fn in selections:            # 構(gòu)建頂層窗口作為對話框            rename_dlg = Toplevel(takefocus=True)            # 指定窗口標(biāo)題            rename_dlg.title('Rename')            # 禁止窗口尺寸調(diào)整            rename_dlg.resizable(width=False, height=False)            # 構(gòu)建Frame對象以容納Label和Entry對象            # 使用Frame可以分別調(diào)整Label/Entry區(qū)域和下面的按鈕區(qū)域            fn_frame = Frame(rename_dlg)            fn_frame.grid(row=0,column=0)            Label(fn_frame, text='File Name:').grid(row=0, column=0)            fn_var = StringVar()            fn_var.set(fn)            fn_entry = Entry(fn_frame, textvariable=fn_var)            fn_entry.grid(row=0, column=1)            # 構(gòu)建Frame對象以容納OK和Cancel按鈕            btn_frame = Frame(rename_dlg)            btn_frame.grid(row=1, column=0, sticky='e')            # 通過labmda表達(dá)式傳遞構(gòu)建按鈕控件時(shí)的對話框控件,路徑和文件名信息            # 修改后的文件名要在按下【OK】按鈕是通過fn_var.get獲取。            ok_btn = Button(btn_frame, text='OK',                            command=(lambda w=rename_dlg,p=path,s=fn: rename_ok(w,p,s,fn_var.get())))            ok_btn.grid(row=0, column=0)            # 取消按鈕直接銷毀窗口對象            cancel_btn=Button(btn_frame, text='Cancel', command=rename_dlg.destroy)            cancel_btn.grid(row=0, column=1)            # 限定rename_dlg接收鼠標(biāo)和鍵盤事件,這是實(shí)現(xiàn)模態(tài)對話框的關(guān)鍵。            rename_dlg.grab_set()            # 使對話框相對于root窗口居中            center_window(rename_dlg, root)            # 啟動(dòng)對話框主循環(huán)            rename_dlg.mainloop()            # 銷毀對話框窗口            rename_dlg.destroy()            # 更新文件列表            select_node(None)

作者編寫了詳細(xì)的注釋行,請結(jié)合代碼自行理解。center_window函數(shù)的功能可以直接在其他場合使用,其實(shí)現(xiàn)如下:

def center_window(wnd, ref):    wnd.update()    width = wnd.winfo_width()    height = wnd.winfo_height()    if ref:        ref_width = ref.winfo_width()        ref_height = ref.winfo_height()        x = ref.winfo_x()        y = ref.winfo_y()        size = '%dx%d+%d+%d' % (width, height, x + (ref_width - width) / 2, y+(ref_height - height) / 2)    else:        s_width = wnd.winfo_screenwidth()        s_height = wnd.winfo_screenheight()        size = '%dx%d+%d+%d' % (width, height, (s_width - width) / 2, (s_height - height) / 2)    wnd.geometry(size)

如果指定了參照窗口對象ref則將窗口調(diào)整到ref的中心位置,否則調(diào)整到屏幕中心位置。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

分享名稱:Python中怎么自定義對話框
本文網(wǎng)址:http://muchs.cn/article38/jcjdsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈企業(yè)網(wǎng)站制作、小程序開發(fā)網(wǎng)站策劃、定制網(wǎng)站域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)