python 3.6 - How to print a function in tkinter -
i made script parses song title ice-cast stream, shows in python shell. have many questions how print text in tkinter gui, not function-text.
how can print in tkinter gui?
here code:
import vlc import os import requests import re stream_url = 'http://oneradio.su:8000/trance' r = requests.get(stream_url, headers={'icy-metadata': '1'}, stream=true) icy_meta_int = r.headers.get('icy-metaint') def gettitle(): chunk in r.iter_content(50): l = str(chunk) match = re.search(r"streamtitle='(.*)';", l) while match none: return none title = match.group(1) print(title) gettitle() root = tk() root.resizable(width=false, height=false) root.wm_title("radio") root.iconbitmap(default='logo.ico') p = vlc.mediaplayer("http://oneradio.su:8000/trance") def play(): p.play() play.configure(state=disabled) def stop(): p.stop() play.configure(state=normal) def hv(): p.audio_set_volume(100) def lv(): p.audio_set_volume(50) logoimg = photoimage(file="logo.png") logo = label(root, image=logoimg) logo.photo = logoimg logo.place(relx=.3, rely=.0) st = label(root, text="now playing:") st.place(relx=.3, rely=.3) play = button(text="play", command=play) play.place(relx=.0, rely=.6) stop = button(text="stop", command=stop) stop.place(relx=.8, rely=.6) hv = button(text="100%", command=hv) hv.place(relx=.6, rely=.6) lv = button(text="50%", command=lv) lv.place(relx=.2, rely=.6) root.mainloop()
you can use tkinter stringvar type , update go, mainloop automatically update label text in stringvar. use get() function data stringvar object, , set(str) set text in stringvar object. use config on label.
import tkinter tk window = tk.tk() def number_range(x): = 1 while < x: yield += 1 generator = number_range(500) stringvar = tk.stringvar() stringvar.set("some placeholder") example_label_one = tk.label(window, textvariable=stringvar) example_label_two = tk.label(window, text="some placeholder") example_label_one.pack() example_label_two.pack() def update_labels(): string = "updated %s times" % generator.next() window.after(500, lambda: stringvar.set(string)) window.after(500, lambda: example_label_two.config(text=string)) window.after(1000, update_labels) # after function inserts command mainloop window.after(500, update_labels) window.mainloop()
Comments
Post a Comment