[問題] 如何用tkinter呈現出print的結果?

看板Python作者 (彼得羊)時間3年前 (2020/09/07 17:13), 編輯推噓1(1016)
留言17則, 3人參與, 3年前最新討論串1/1
各位先進大家好,請問要如何使用tkinter, 讓print得到的結果, 能夠在GUI視窗介面內顯示出來呢? 謝謝 程式碼如下: import tkinter as tk win = tk.Tk() win.title("乘法") win.geometry("200x200") note = tk.Label(text="點擊得到結果") note.pack() def times(): for i in range(10, 0, -1): note.config(text="結果") print(5, "x", i, "=", 5*i) btn = tk.Button(text="點擊") btn.config(command=times) btn.pack() tk.mainloop() 點擊按鈕後,print的結果要如何顯示在視窗內 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.231.138.192 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1599470038.A.AB8.html

09/07 20:18, 3年前 , 1F
在 "def times():" 這個函數中的for loop加上:
09/07 20:18, 1F

09/07 20:18, 3年前 , 2F
s = str(5) + "x" + str(i) + "=" + str(5*i)
09/07 20:18, 2F

09/07 20:19, 3年前 , 3F
n = tk.Label(text = s)
09/07 20:19, 3F

09/07 20:19, 3年前 , 4F
n.pack()
09/07 20:19, 4F

09/07 20:19, 3年前 , 5F
這三行都要放在for loop 裡面
09/07 20:19, 5F

09/07 20:45, 3年前 , 6F
這程式碼應該不通吧。
09/07 20:45, 6F

09/07 20:46, 3年前 , 7F
所有tk.XXX宣告都沒有把win傳進去
09/07 20:46, 7F

09/07 20:48, 3年前 , 8F
我錯了,原來可以這樣寫..
09/07 20:48, 8F

09/07 20:50, 3年前 , 9F
你的問題應該是要找個元件可以放字串,隨便找一下就
09/07 20:50, 9F

09/07 20:50, 3年前 , 10F
有了吧
09/07 20:50, 10F

09/07 20:58, 3年前 , 11F
def times():
09/07 20:58, 11F

09/07 20:58, 3年前 , 12F
s=''
09/07 20:58, 12F

09/07 20:58, 3年前 , 13F
for i in range(10, 0, -1):
09/07 20:58, 13F

09/07 20:58, 3年前 , 14F
s += f'5 * i = {5*i}\n'
09/07 20:58, 14F

09/07 20:59, 3年前 , 15F
note.config(text=s)
09/07 20:59, 15F

09/07 20:59, 3年前 , 16F
直接把字串放到note(ps.要用python3跑)
09/07 20:59, 16F

09/16 23:54, 3年前 , 17F
謝謝大家的推文~
09/16 23:54, 17F
文章代碼(AID): #1VLVdMgu (Python)