如何在文本PYTHON中插入两个以上的变量

时间:2018-09-06 05:03:58

标签: python variables canvas text

我在文本中插入两个变量时遇到问题。你能帮我吗?逗号或&分隔不起作用。

from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
import tkinter as tk
import random
import time
a=7
cluster=10
window = tk.Tk()
window.title("Ainalaiyn Task Manager")
window.geometry("1200x600+0+0")
canvas1 = tk.Canvas(window, width=200, height=200, bg="bisque", bd=5)
canvas1.pack(side="top", fill="both")
canvas1.create_text(10, 70, anchor="sw", fill="black",font="Times 15 italic", text="There is an upcoming task in %d days at %d cluster!" %a %cluster)
window.mainloop()

3 个答案:

答案 0 :(得分:0)

尝试一下:

  1. somestring = "There is an upcoming task in %d days at %d cluster!" % (a, cluster)
  2. 或使用格式字符串。 somestring = "There is an upcoming task in {0} days at {1} cluster!".format(a, cluster)

答案 1 :(得分:0)

所有这些工作(仅最后一项> 3.6):

"There is an upcoming task in %d days at %d cluster!" % (a, cluster)

"There is an upcoming task in {0} days at {1} cluster!".format(a, cluster)

f"There is an upcoming task in {a} days at {cluster} cluster!"

答案 2 :(得分:0)

当前Python中正确的方法是:

somestring = f”there is an upcoming task in {a} days at {cluster} cluster!”