简体   繁体   English

如何在python中为随机形状生成随机坐标

[英]How to generate random cordinates for a random shapes in python

This is the code for the program I'm trying to get random coordinates for to draw the random shape now the random color works but when i run the program i get this error: 这是我试图获取随机坐标以绘制随机形状的程序的代码,现在随机颜色有效,但是当我运行该程序时出现此错误:

(Traceback (most recent call last):
  File "/home/04danielbrew/Desktop/Python/Random Coloured Square.py", line 9, in <module>
    randomh=random.randint(1,1080)
AttributeError: 'builtin_function_or_method' object has no attribute 'randint)

And thanks in advance and I don't want the most advanced code as I am only doing GCSE computer science. 在此先感谢您,我不需要最高级的代码,因为我只是在做GCSE计算机科学。

from tkinter import*
from random import*
####Set veriables
colors=["mintcream","orangered","gold","coral","red", "orange", "yellow", "green", "blue","violet","midnightblue"]
canvas_height=1080
canvas_width=1920
canvas_colour='black'
line_width=1
randomh=random.randint(1,1080)
randomw=random.randint(1,1920)
randomh2=random.randint(1,1080)
randomw2=random.randint(1,1920)
####Define function
def square(self):
     canvas.create_line(randomh,randomw,randomh2,randomw2, width=line_width,          fill=random.choice(colors))
     canvas.create_line(300,100,400,100, width=line_width, fill=random.choice(colors))
     canvas.create_line(400,100,400,200, width=line_width, fill=random.choice(colors))
     canvas.create_line(400,200,300,200, width=line_width, fill=random.choice(colors))
###Main program
window=Tk()
window.title("Daniel Random Amazballs lines")
canvas=Canvas(bg=canvas_colour,height=canvas_height,width=canvas_width, highlightthickness=0)
canvas.pack()
###Click in window to start
window.bind("<Button-1>",square)
window.mainloop()

As a best practice, and for Pythonic code, don't from <module> import * . 作为最佳实践,对于Python代码,请不要from <module> import *

In this case, you could use from random import randint, choice instead. 在这种情况下,您可以使用from random import randint, choice

Then your calls would look something like: randomh = randint(1,1080) and choice(colors) 那么您的呼叫将类似于: randomh = randint(1,1080)choice(colors)

The point is that from <module> import * is almost always the wrong way - it pollutes your top-level namespace with everything from <module> . 关键是from <module> import *几乎总是错误的方式-它使用<module> 所有内容污染您的顶级名称空间。

Explicit is better than implicit 显式胜于隐式

This isn't about advanced code, this is about learning to write code that clearly expresses your intent. 这与高级代码无关,而与学习编写清楚表达您意图的代码有关。

Since you are importing everything from random you don't need to call random.randint(), you just have to call randint() direcly. 由于您要从random导入所有内容,因此无需调用random.randint(),只需直接调用randint()即可。

like so: 像这样:

randomh = randint(1,1080)
randomw = randint(1,1920)
randomh2 = randint(1,1080)
randomw2 = randint(1,1920)

As gomad said, it's not a good practice importing everything from a module, especially if you are using only a single method. 正如gomad所说,从模块导入所有内容不是一个好习惯,尤其是在仅使用一种方法的情况下。

Also, random is supposed to be used with just import. 此外,随机值仅应与导入一起使用。 See the difference in: 看到不同之处:

import random
random.choice([1,2,3,4])

and

from random import choice
choice([1,2,3,4])

The first one is clear what it is doing, it chooses one element in the array. 第一个很清楚它在做什么,它在数组中选择一个元素。 While the second is unclear, does it chose the array as something? 虽然第二个还不清楚,但它是否选择了数组作为对象? Does it present the choices to the user? 是否向用户呈现选择?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM