【发布时间】:2021-06-01 18:50:23
【问题描述】:
我刚开始学习 Tkinter。通过一个初学者项目,我遇到了这种情况,我必须使用相同的按钮来生成一个字符串并用一个新的替换它,因为新的与旧的重叠。以下是代码段:
def ranQ():
if root.count < len(root.LoQ):
tx=canv.create_text(400, 50, text=root.LoQ[root.count], font=('Jokerman', 15), fill="Purple")
root.count += 1
else:
# l2 = Label(root, text="End of 7 questions! Click to generate again!", fg= "white", bg ="red")
# l2.pack()
canv.create_text(400, 450, text="End of 7 questions! Click to generate again!", font=('Jokerman', 10), fill="Purple")
root.count=0
random.shuffle(root.LoQ)
# defining background image
im = PhotoImage(file="{path}")
# Creating a canvas
canv = Canvas(root, width =800, height=500)
canv.pack(fill ="both", expand= True)
# Putting the image in canvas
canv.create_image(0,0, image=im, anchor='nw')
# adding a text label
canv.create_text(400,450,text ="7 Random Questions!", font=('Jokerman',50), fill="black")
canv.create_text(404,454,text ="7 Random Questions!", font=('Jokerman',50), fill="purple")
# adding the generate button
btn_g = Button(root, text='Press to generate a random question'.upper(),font=('Jokerman',10), padx=60, pady=20, fg="white", bg='Red',
command=ranQ)
b_window = canv.create_window(220,225,anchor="nw", window= btn_g)
【问题讨论】:
标签: python tkinter tkinter-canvas