我正在尝试在Python中以不同的方向显示网格,还有另一种方法可以垂直,透视等方式执行此操作吗?我有时也由于某种原因说“在分配之前引用了局部变量'错误'”,但是在另一个Flask程序中,这并不是我遇到的问题。当前,网格水平显示颜色。
编辑-所以我设法解决了分配错误之前引用的问题,事实证明这是一个缩进问题。我试图更改网格上的颜色,将值与带有颜色的变量代入,这无济于事。我还在POST请求方法下添加了if请求方法=='GET',并指出如果发出了get请求,则中止。
关于如何在POST请求上重新安排网格,我还是一无所知。我添加了request.form ['type.h-stripes'],当我按照文档中的示例进行操作时,该响应返回错误响应,并且未定义随机播放。
from flask import Flask, render_template, request
import time
import random
app = Flask(__name__) # creates applications instance
app.config.from_object(__name__) # loads configuration from this file, flaskr.py
@app.route('/', methods=['GET', 'POST'])
def homepage():
#print ("time.time(): %f " % time.time())
#print (time.localtime(time.time()))
curent_time = (time.asctime(time.localtime(time.time())))
if request.method == 'POST':
error = None
type = request.form['type'] # takes the name value in the html
# Colour 4 being the last colour specified in the array, cahnging displaycolur1 (as an example) to #000000 will force the colour to display as a black colour as it is in hex value
randomcolourr1 = lambda: random.randint(0,255)
displaycolu1 = ('#%02X%02X%02X' % (randomcolourr1(),randomcolourr1(),randomcolourr1()))
randomcolourr2 = lambda: random.randint(0,255)
displaycolu2 = ('#%02X%02X%02X' % (randomcolourr2(),randomcolourr2(),randomcolourr2()))
randomcolourr3 = lambda: random.randint(0,255)
displaycolu3 = ('#%02X%02X%02X' % (randomcolourr3(),randomcolourr3(),randomcolourr3()))
randomcolourr4 = lambda: random.randint(0,255)
displaycolu4 = ('#%02X%02X%02X' % (randomcolourr4(),randomcolourr4(),randomcolourr4()))
randomcolourr5 = lambda: random.randint(0,255)
displaycolu5 = ('#%02X%02X%02X' % (randomcolourr5(),randomcolourr5(),randomcolourr5()))
*EDIT* if request.form['type.h-stripes']: # if request.form contains the type name and h-stripes value being selected
tablehori = ('color0, color1, color2, color3, color4'.split()) # takes colour values from the table td elements
shuffle(tablehori) # not defined
print (tablehori) # prints to console
return render_template('index-example.html', error=error, curent_time=curent_time, displaycolu1=displaycolu1, displaycolu2=displaycolu2, displaycolu3=displaycolu3, displaycolu4=displaycolu4, displaycolu5=displaycolu5)
if __name__ == '__main__':
app.run(debug=True)
<form method="POST">
<div class="settingpanel">
<label for="color_0">Color 1</label>
<input type="color" name="color0" id="color_0" value="{{ displaycolu1 }}">
<label for="color_1">Color 2</label>
<input type="color" name="color1" id="color_1" value="{{ displaycolu2 }}">
<label for="color_2">Color 3</label>
<input type="color" name="color2" id="color_2" value="{{ displaycolu3 }}">
<label for="color_3">Color 4</label>
<input type="color" name="color3" id="color_3" value="{{ displaycolu4 }}">
<label for="color_4">Color 5</label>
<input type="color" name="color4" id="color_4" value="{{ displaycolu5 }}">
</div>
<div class="settingpanel">
<input type="radio" name="type" value="h-stripes"
id="type_h-stripes" checked >
<label for="type_h-stripes">h-stripes</label>
<input type="radio" name="type" value="v-stripes"
id="type_v-stripes" >
<label for="type_v-stripes">v-stripes</label>
<input type="radio" name="type" value="carpet"
id="type_carpet" >
<label for="type_carpet">carpet</label>
<input type="radio" name="type" value="random"
id="type_random" >
<label for="type_random">random</label>
<input type="radio" name="type" value="diagonal"
id="type_diagonal" >
<label for="type_diagonal">diagonal</label>
</div>
<input type="submit" value="show">
</form>