第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python Tkinter 刪除函數(shù)中的條目

Python Tkinter 刪除函數(shù)中的條目

PHP
忽然笑 2023-11-09 15:50:15
我正在嘗試制作一個(gè)應(yīng)用程序來顯示不同時(shí)區(qū)的時(shí)間。如果條目不在 pytz.all_timezones 中,并且用戶輸入了不包含逗號(hào)的內(nèi)容,我嘗試生成消息框錯(cuò)誤。該應(yīng)用程序似乎一直有效,直到我開始刪除該條目,并且它出現(xiàn)了一個(gè)我不想要的消息框。另外,如何讓時(shí)間不受條目更改的影響,直到再次按下搜索按鈕,因?yàn)橐坏┪议_始刪除條目,時(shí)間就會(huì)停止更新。謝謝import tkinter as tkfrom tkinter import *from tkinter import messageboxfrom datetime import datetimeimport pytzimport timeroot=tk.Tk()root.title('Time app')def times():    a = entry_1.get()    try:        b = a.index(',')        c = a[b + 2:] + '/' + a[:b]        if c in pytz.all_timezones:            home = pytz.timezone(c)            local_time = datetime.now(home)            current_time = local_time.strftime('%H:%M:%S')            place_lbl = Label(root, text=a, bg='grey', width=15, font=('bold', 25))            place_lbl.place(relx=0.33, rely=0.4)            time_lbl = Label(root, text=current_time, bg='grey', font=('bold', 30))            time_lbl.place(relx=0.41, rely=0.5)            time_lbl.after(200,times)                   else:            messagebox.showerror('Error',"Cannot find '{}'. Please enter in form city, continent (e.g. London, Europe).".format(a))    except:        messagebox.showerror('Error',"Cannot find '{}'. Please enter in form city, continent (e.g. London, Europe).".format(a))    entry_1.delete(first=0, last=20)canvas=tk.Canvas(root,height=400,width=700,bg='grey')canvas.grid()header_lbl=Label(root,text='Enter a city: ',bg='grey',fg='black',font=('bold',25))header_lbl.place(relx=0.41,rely=0.1)entry_1=Entry(root)entry_1.place(relx=0.37,rely=0.2)search_btn=Button(root,text='Search',command=times)search_btn.place(relx=0.47,rely=0.3)root.mainloop()
查看完整描述

1 回答

?
開心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊

這是我解決這個(gè)問題的方法,可能還有其他方法。


def value():

    global a

    a = entry_1.get()


def times():

    try:

        b = a.index(',')

        c = a[b + 2:] + '/' + a[:b]

        if c in pytz.all_timezones:

            home = pytz.timezone(c)

            local_time = datetime.now(home)

            current_time = local_time.strftime('%H:%M:%S')


            place_lbl = Label(root, text=a, bg='grey', width=15, font=('bold', 25))

            place_lbl.place(relx=0.33, rely=0.4)

            time_lbl = Label(root, text=current_time, bg='grey', font=('bold', 30))

            time_lbl.place(relx=0.41, rely=0.5)

            time_lbl.after(1000,times)

        else:

            messagebox.showerror('Error',"Cannot find '{}'. Please enter in form city, continent (e.g. London, Europe).".format(a))

    except:

        messagebox.showerror('Error',"Cannot find '{}'. Please enter in form city, continent (e.g. London, Europe).".format(a))

確保將按鈕更改為:


search_btn = Button(root,text='Search',command=lambda:[value(),times(),entry_1.delete(0, END)])

問題是您使用的after(),但一旦代碼運(yùn)行,輸入框?yàn)榭?,然后您將使用框?nèi)刪除的值。因此,我最初將值傳遞給另一個(gè)函數(shù),以便將其存儲(chǔ)在那里。


通過按鈕的命令,我調(diào)用三個(gè)函數(shù),第一個(gè)函數(shù)value()將具有條目小部件中的值,然后下times()一個(gè)是刪除條目小部件。如果您將刪除包含在其中,times()那么它將導(dǎo)致該條目每秒被刪除。


當(dāng)您也想搜索更多城市時(shí),這很有效。



查看完整回答
反對(duì) 回復(fù) 2023-11-09
  • 1 回答
  • 0 關(guān)注
  • 205 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)