同一画布上可以有两个对象吗?

时间:2019-02-10 17:13:46

标签: python-3.x tkinter-canvas

我正在尝试学习tkinter并继续陷于在同一画布上包含两个项目的问题。我可以在底部有按钮或矩形,但不能两者都有。我还没有尝试过网格,但是我尝试过以不同的方式打包它们并重新排列所有内容。谢谢。

#!/usr/bin/python
import time
import tkinter as tk
import random as rand

gui = tk.Tk()
gui.geometry("800x800")
can = tk.Canvas(gui ,width=800 ,height=800)
gui.title("Moving Squares")


def button_clicked(event):
    #copy text from textbox and place into label
    print(f"the button is clicked, text.get() = {text.get()}")
    label_string.set(text.get())


label_string = tk.StringVar()
label_string.set("nothing?")
label = tk.Label(can,textvariable=label_string)

text = tk.Entry(can)
text_string = 'hello?'
text.insert(3, text_string)

button = tk.Button(can, text='Copy text from textbox')
button.bind("<1>", button_clicked)
# stands for  ^  button 1 aka mouse 1



large_square = can.create_rectangle(10,400,780,760, fill="yellow")
small_square = can.create_rectangle(-780,400,20,760, fill= 'blue')
can.pack()
label.pack()
text.pack()
button.pack()
for i in range(10): 
    count = 20
    time.sleep(1)
    random_num = rand.randint(-50,50)
    can.move(small_square,random_num,0)
    count+100
    gui.update()
can.move(small_square,780- abs(random_num), 0)
gui.mainloop()

0 个答案:

没有答案