当前位置 : 主页 > 编程语言 > python >

PythonGUI编程之移动控件

来源:互联网 收集:自由互联 发布时间:2022-06-24
移动小球 xx = 470 yy = 350 x = xx / / 2 y = yy / / 2 def tkinter3 (): root = tk . Tk () r = 10 step = 10 root . geometry ( str ( xx ) + 'x' + str ( yy ) + '+250+250' ) root . resizable ( False , False ) root . title ( "手动移动小球

移动小球

xx = 470
yy = 350
x = xx // 2
y = yy // 2
def tkinter3():
root = tk.Tk()
r = 10
step = 10
root.geometry(str(xx)+'x'+str(yy)+'+250+250')
root.resizable(False, False)
root.title("手动移动小球")
canvas = tk.Canvas(root, bg='white', height=700, width=700)
canvas.pack()
circle = canvas.create_oval(x + r, y + r, x - r, y - r, fill='red')
def judge(x, y):
if 0<= x <= xx and 0 <= y <= yy:
return True
else:
return False
def left():
global x, y
x -= step
if judge(x, y):
canvas.move(circle, -step, 0)
leftBtn = tk.Button(root, text="左移", width=8, height=1)
leftBtn.config(command=left)
leftBtn.place(x=75, y=320)
def right():
global x, y
x += step
if judge(x, y):
canvas.move(circle, step, 0)
rightBtn = tk.Button(root, text="右移", width=8, height=1)
rightBtn.config(command=right)
rightBtn.place(x=150, y=320)
def up():
global x, y
y -= step
if judge(x, y):
canvas.move(circle, 0, -step)
upBtn = tk.Button(root, text="上移", width=8, height=1)
upBtn.config(command=up)
upBtn.place(x=225, y=320)
def down():
global x, y
y += step
if judge(x, y):
canvas.move(circle, 0, step)
downBtn = tk.Button(root, text="下移", width=8, height=1)
downBtn.config(command=down)
downBtn.place(x=300, y=320)
root.mainloop()

PythonGUI编程之移动控件_python

移动标签

x = 0

import tkinter as tk
import random
def no3():
"""
:return:
"""
root = tk.Tk()
root.title("标签定制")
root.geometry('320x160+250+250')
root.resizable(False, False)
v = tk.IntVar()
tk.Label(root, text='标\n签\n定\n制', fg="red", width=15,
height=5).place(x=10, y=80, anchor=tk.CENTER)

values = [('红色', 0, 'red'), ('黄色', 1, 'yellow'), ('白色', 2, 'white'),
('绿色', 3, 'green'),
('随机色', 4, "#" + (''.join([hex(random.randint(17, 256)) for i in
range(3)])).replace('0x', ''))]
def callRB():
global x, t
x = 160
for i in range(5):
if v.get() == i:
t = tk.Label(root, text='Python Tkinter', bg=values[i][2],
width=15, height=3)
t.place(x=x, y=80, anchor=tk.CENTER)
t = 30
for i in range(5):
r = tk.Radiobutton(root, text=values[i][0], font=("Arial", 12),
command=callRB)
r.config(variable=v, value=values[i][1])
r.place(x=t, y=20, anchor=tk.CENTER)
t += 60

def left():
global x, t
x -= 10
for i in range(5):
if v.get() == i:
t.destroy()
t = tk.Label(root, text='Python Tkinter', bg=values[i][2],
width=15, height=3)
t.place(x=x, y=80, anchor=tk.CENTER)
leftBtn = tk.Button(root, text="左移", width=8, height=1)
leftBtn.config(command=left)
leftBtn.place(x=75, y=120)

def right():
global x, t
x += 10
for i in range(5):
if v.get() == i:
t.destroy()
t = tk.Label(root, text='Python Tkinter', bg=values[i][2],
width=15, height=3)
t.place(x=x, y=80, anchor=tk.CENTER)
rightBtn = tk.Button(root, text="右移", width=8, height=1)
rightBtn.config(command=right)
rightBtn.place(x=175, y=120)

root.mainloop()
no3()


上一篇:Python小记——__call__方法
下一篇:没有了
网友评论