[英]Can I create random x and y coordinates for a button - Tkinter Python
import tkinter as tk
import random
root = tk.Tk()
canvas = tk.Canvas(root, height=600, width=700, bg="#4f75b3")
canvas.pack()
frame = tk.Frame(root, bg="#66bd5e")
frame.place(relx=0.075, rely=0.075, relheight=0.85, relwidth=0.85,)
def destroymole():
mole.destroy()
mole = tk.Button(root,text="MOLE", relief="raised", command=destroymole, x=random.randint(300,700), y=random.randint(300, 700), height=20, width=30)
root.mainloop()
运行后我得到这个错误
Traceback (most recent call last): File
"C:\Users\\PycharmProjects\pythonProject\GUI APP.py", line 15, in
<module>
mole = tk.Button(root, text="MOLE",relief="raised", command=destroymole, x=random.randint(300,700),y=random.randint(300,
700), height=20, width=30) File
"C:\Users\\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py",
line 2647, in __init__
Widget.__init__(self, master, 'button', cnf, kw) File "C:\Users\\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py",
line 2569, in __init__
self.tk.call(
_tkinter.TclError: unknown option "-x"
但是,主要的是最后一行,我很确定它与随机数有关
如果还有其他明显的问题,请告诉我,因为这对我来说都是一个谜。
您尚未放置按钮。 希望,您知道可以通过三种方式在 window(放置、包装、网格)中设置小部件。 我使用了place方法并在参数中传递了随机值。
import tkinter as tk
import random
root = tk.Tk()
canvas = tk.Canvas(root, height=600, width=700, bg="#4f75b3")
canvas.pack()
frame = tk.Frame(root, bg="#66bd5e")
frame.place(relx=0.075, rely=0.075, relheight=0.85, relwidth=0.85,)
def destroymole():
mole.destroy()
mole = tk.Button(root,text="MOLE", relief="raised", command=destroymole)
mole.place(x=random.randint(300,700), y=random.randint(300, 700), height=20, width=30)
root.mainloop()
Rest 如有任何疑问,请随时问我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.