Python tkinter: Make any output appear in a text box on GUI not in the shell -
i making gui using python , tkinter , wondering if there anyway create output text appear in window on gui not on interpreter/shell?
thanks in advance
if, suggested in bryan oakley's comment, want “print 'foo' in gui, have magically appear in text widget”, see answers in previous question python : converting cli gui. reply addresses simpler issue of how produce output in text box. produce scrolling text window, create , place or pack text widget (let's phone call mtb
), utilize commands mtb.insert(tkinter.end, ms)
add together string ms
text box mtb
, , mtb.see(tkinter.end)
create box scroll. (see “the tkinter text widget” documentation more details.) example:
#!/usr/bin/env python import tkinter tk def cbc(id, tex): homecoming lambda : callback(id, tex) def callback(id, tex): s = 'at {} f {}\n'.format(id, id**id/0.987) tex.insert(tk.end, s) tex.see(tk.end) # scroll if necessary top = tk.tk() tex = tk.text(master=top) tex.pack(side=tk.right) bop = tk.frame() bop.pack(side=tk.left) k in range(1,10): tv = 'say {}'.format(k) b = tk.button(bop, text=tv, command=cbc(k, tex)) b.pack() tk.button(bop, text='exit', command=top.destroy).pack() top.mainloop()
note, if expect text window remain open long periods and/or accumulate gigabytes of text, perhaps maintain track of how much info in text box, , utilize delete
method @ intervals limit it.
python tkinter
No comments:
Post a Comment