我使用python和Tkinter GUI为我当地的火车站创建了一个全屏离场板,我已经把它连接到树莓派。
为了节省空间,我已将列车目的地显示的字符数限制为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
正如你所看到的," Hertford North" "韦林花园城"不适合所提供的空间。我想要在当前文本小部件的连续循环中勾选这些名称。
为凌乱的剧本道歉,我有点像菜鸟
答案 0 :(得分:1)
以下是如何从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()
P.S这不是我的代码vegaseat's代码