返回列表框中的输出,TKinter

时间:2018-01-06 19:11:54

标签: python python-3.x user-interface tkinter

我正在尝试使用TKinter创建一个响应来自用户的时区输入的时钟。我在代码中遇到selectTZ函数时遇到问题。它打印选定的时区,但是当我尝试返回它并在时钟功能中使用它时,它会出错。你能帮我弄清问题是什么吗?

import datetime
import tkinter
import pytz

# creates the main clock window
mainWindow = tkinter.Tk()
mainWindow.geometry('600x300')
mainWindow['pady'] = 8
mainWindow['padx'] = 8

# cnfig rows and columns
mainWindow.columnconfigure(0, weight=1)
mainWindow.columnconfigure(1, weight=1)
mainWindow.columnconfigure(2, weight=100)
mainWindow.columnconfigure(3, weight=100)
mainWindow.columnconfigure(4, weight=100)
mainWindow.rowconfigure(0, weight=1)
mainWindow.rowconfigure(1, weight=1)
mainWindow.rowconfigure(2, weight=1)
mainWindow.rowconfigure(3, weight=1)
mainWindow.rowconfigure(4, weight=1)

# frame for the date and time display
timeDateFrame = tkinter.Frame(mainWindow, relief='sunken', borderwidth=1)
timeDateFrame.grid(row=0, column=2)


def selectTZ(e):
    selection = (TZlist.curselection())
    TZ = TZlist.get(selection)
    TZ = pytz.timezone(TZ)
    print(TZ)


# list of time zones
TZlist = tkinter.Listbox(mainWindow, relief='sunken', borderwidth=1, selectmode='SINGLE', exportselection=False)
for i in pytz.all_timezones:
    TZlist.insert(tkinter.END, i)
TZlist.grid(row=0, column=0, sticky='ns', rowspan=5)
TZlist.bind('<<ListboxSelect>>', selectTZ)



# scrollbar for TZ list
TZscroll = tkinter.Scrollbar(mainWindow, orient=tkinter.VERTICAL, command=TZlist.yview)
TZscroll.grid(row=0, column=1, rowspan=5, sticky='ns')
TZlist['yscrollcommand'] = TZscroll.set


# creates the time display
def clock():
    timeNow = datetime.datetime.strftime(datetime.datetime.now(), '%X')
    timeLable = tkinter.Label(timeDateFrame, text=timeNow)
    timeLable.grid(row=0, column=0)
    mainWindow.after(1000, clock)


# create the date display
def date():
    dateNow = datetime.datetime.strftime(datetime.datetime.now(), '%x')
    dateLable = tkinter.Label(timeDateFrame, text=dateNow)
    dateLable.grid(row=1, column=0)
    mainWindow.after(1000, date)


mainWindow.after(1, date)
mainWindow.after(1, clock)
mainWindow.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以使用两个StringVar来更新标签,而不是每秒创建两个标签。要使用datetime.datetime.now (pytz.timezone ())

获取特定时区的当前日期时间
import datetime
import tkinter
import pytz



# creates the main clock window
mainWindow = tkinter.Tk()
mainWindow.geometry('600x300')
mainWindow['pady'] = 8
mainWindow['padx'] = 8


TZ = tkinter.StringVar(mainWindow, pytz.all_timezones[0])
timeNow = tkinter.StringVar(mainWindow)
dateNow = tkinter.StringVar(mainWindow)

# cnfig rows and columns
mainWindow.columnconfigure(0, weight=1)
mainWindow.columnconfigure(1, weight=1)
mainWindow.columnconfigure(2, weight=100)
mainWindow.columnconfigure(3, weight=100)
mainWindow.columnconfigure(4, weight=100)
mainWindow.rowconfigure(0, weight=1)
mainWindow.rowconfigure(1, weight=1)
mainWindow.rowconfigure(2, weight=1)
mainWindow.rowconfigure(3, weight=1)
mainWindow.rowconfigure(4, weight=1)


# frame for the date and time display
timeDateFrame = tkinter.Frame(mainWindow, relief='sunken', borderwidth=1)
timeDateFrame.grid(row=0, column=2)
timeLabel = tkinter.Label(timeDateFrame, textvariable=timeNow)
timeLabel.grid(row=0, column=0)
dateLabel = tkinter.Label(timeDateFrame, textvariable=dateNow)
dateLabel.grid(row=1, column=0)


def selectTZ(e):
    selection = (TZlist.curselection())
    TZ.set(TZlist.get(selection))


# list of time zones
TZlist = tkinter.Listbox(mainWindow, relief='sunken', borderwidth=1, selectmode='SINGLE', exportselection=False)
for i in pytz.all_timezones:
    TZlist.insert(tkinter.END, i)
TZlist.grid(row=0, column=0, sticky='ns', rowspan=5)
TZlist.bind('<<ListboxSelect>>', selectTZ)


# scrollbar for TZ list
TZscroll = tkinter.Scrollbar(mainWindow, orient=tkinter.VERTICAL, command=TZlist.yview)
TZscroll.grid(row=0, column=1, rowspan=5, sticky='ns')
TZlist['yscrollcommand'] = TZscroll.set


# update date and time
def update():
    dt = datetime.datetime.now(pytz.timezone(TZ.get()))
    timeNow.set(datetime.datetime.strftime(dt, '%X'))
    dateNow.set(datetime.datetime.strftime(dt, '%x'))
    mainWindow.after(1000, update)


mainWindow.after(1, update)
mainWindow.mainloop()

enter image description here