火车离站票

2024-09-28 22:10:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用python和tkintergui为我的本地火车站创建了一个全屏的离港板,并将其连接到了一个覆盆子pi上。在

为了节省空间,我把显示列车目的地的字符数限制在13个以内。我想使这个滚动在一个股票风格,以便阅读目的地超过13个字符。在

请告诉我,如果你有任何想法,使火车目的地滚动?在

我已经在下面的代码中为国家铁路数据库清空了API密钥。如果你想让它测试程序,请问。在

import Tkinter as tk
from nredarwin.webservice import DarwinLdbSession
from datetime import datetime


top = tk.Tk()
top.title("Departure Board for Harringay Station")
top.configure(background='black')

def callback():
    darwin_session = DarwinLdbSession(wsdl='https://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2015-05-14', api_key = 'xxxxx-xxxxx-xxxxxx-xxxxxxx')
    crs_code = "HGY"
    board = darwin_session.get_station_board(crs_code)
    tex.delete(1.0, tk.END)

    s = "\nNext departures for %s" % (board.location_name)
    t = """
-------------------------------
|P| DEST         |SCHED|  DUE  |
------------------------------- \n"""
    tex.insert(tk.END, s + t, 'tag-center')
    tex.see(tk.END)

    for service in board.train_services:
        u = ("|%1s|%14s|%5s|%7s|\n" %(service.platform or "", service.destination_text[:13], service.std, service.etd[:7]))
        tex.insert(tk.END, u, 'tag-center')   

    v = "--------------------------------\n"
    tex.insert(tk.END, v, 'tag-center')

    tex.after(10000, callback)

def tick():
    tex2.delete(1.0, tk.END)
    now = datetime.now()
    tex2.insert(tk.END, "%s %s %s %s %s:%s" %(now.strftime('%A'), now.strftime('%d'), now.strftime('%b'), now.strftime('%Y'), now.strftime('%H'), now.strftime('%M')), 'tag-center')
    tex2.after(1000, tick)


def close_window (): 
    top.destroy()

button = tk.Button(top, text = "Exit", highlightthickness=0, command = close_window, bg = "black", fg = "orange")
button.pack(side = tk.TOP, anchor = tk.NE)



tex2 = tk.Text(master=top, font = "Courier 28 bold", highlightthickness=0, cursor = 'none', insertwidth=0, height = 1, bg = "black", fg = "orange", borderwidth = 0)
tex2.pack(side = tk.TOP)
tex2.tag_configure('tag-center', justify='center')

tex = tk.Text(master=top, font = "Courier 25 bold", highlightthickness=0, cursor = 'none', bg = "black", fg = "orange", borderwidth = 0)
tex.pack()
tex.tag_configure('tag-center', justify='center')

w, h = top.winfo_screenwidth(), top.winfo_screenheight()
top.overrideredirect(1)
top.geometry("%dx%d+0+0" % (w, h))

callback()
tick()

top.mainloop()

这将产生以下输出(单击下面的图片):

Tkinter train departures board

正如你所见,“赫特福德北”和“威尔温花园城市”不适合提供的空间。我希望这些名称在当前文本小部件中连续循环。在

抱歉,我的剧本太乱了,我有点傻了


Tags: importboardtoptagservicenowtkend
1条回答
网友
1楼 · 发布于 2024-09-28 22:10:29

以下是如何从Dani Web制作股票行情的示例:

''' Tk_Text_ticker102.py
using Tkinter to create a marquee/ticker
uses a display width of 20 characters
not superbly smooth but good enough to read
tested with Python27 and Python33  by  vegaseat  04oct2013
'''
import time
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
root = tk.Tk()
# width  > width in chars, height  > lines of text
text_width = 20
text = tk.Text(root, width=text_width, height=1, bg='yellow')
text.pack()
# use a proportional font to handle spaces correctly
text.config(font=('courier', 48, 'bold'))
s1 = "We don't really care why the chicken crossed the road.  "
s2 = "We just want to know if the chicken is on our side of the "
s3 = "road or not. The chicken is either for us or against us.  "
s4 = "There is no middle ground here.  (George W. Bush)"
# pad front and end of text with spaces
s5 = ' ' * text_width
# concatenate it all
s = s5 + s1 + s2 + s3 + s4 + s5
for k in range(len(s)):
    # use string slicing to do the trick
    ticker_text = s[k:k+text_width]
    text.insert("1.1", ticker_text)
    root.update()
    # delay by 0.22 seconds
    time.sleep(0.22)
root.mainloop()

这不是我的代码,这是vegaseat's代码

相关问题 更多 >