I am getting some voltage value from the box and based on that I need to update the text of all 32 text labels one by one.
if switch == 'J1':
if StatusPT[0] == True and StatusLED[0] == True:
self.color[0] = Qt.green
self.StatusJ1.setText('Pass')
if StatusPT[0] == False:
self.color[0] = Qt.red
self.StatusJ1.setText(StatusPT[1])
elif StatusLED[0] == False:
self.color[0] = Qt.red
self.StatusJ1.setText(StatusLED[1])
if switch == 'J2':
if StatusPT[0] == True and StatusLED[0] == True:
self.color[1] = Qt.green
self.StatusJ2.setText('Pass')
elif StatusPT[0] == False:
self.color[1] = Qt.red
self.StatusJ2.setText(StatusPT[1])
elif StatusLED[0] == False:
self.color[1] = Qt.red
self.StatusJ2.setText(StatusLED[1])
so I have 32 self.Status(J1-J32).setText
is there any way I do for loop to update them?
Related
I am making a very creative tetris clone for a project with a friend and we have custom sprites for every shape (including the rotations) and we made a wall of else if statements for the rotations. It's supposed to work like this: It calls a random shape out of list of the main 7 shapes and every time the user presses "a" it changes the left facing gif of that shape using the giant el if wall. However, we ran unto an error where instead of executing the elif statements, it just skips to the last line and changes to the J shape no matter what shape was called in the random statement. Any help would be awesome, thank you! (We are still learning btw) also the rotate function is also supposed to happen when the user presses the "d" key but its supposed to change to the other direction.
import turtle as trtl
import random as rand
wn = trtl.Screen()
#configurations
tetris = trtl.Turtle()
drawer = trtl.Turtle()
background = trtl.Turtle()
starty = int(-400)
square = ("sqaure_block.gif")
s_block_normal = ("S_block_norm.gif")
s_block_standing = ("S_block_right.gif")
invert_s_block_normal = ("Z_block_norm.gif")
invert_s_block_standing = ("Z_block_right.gif")
l_block_normal = ("L_block_norm.gif")
l_block_180 = ("L_block_180.gif")
l_block_right = ("L_block_right.gif")
l_block_left = ("L_block_left.gif")
line_normal = ("line_block_norm.gif")
line_standing = ("line_block_standing.gif")
t_block_normal = ("T_block_norm.gif")
t_block_180 = ("T_block_180.gif")
t_block_right = ("T_BLOCK_RIGHT.gif")
t_block_left = ("T_block_left.gif")
j_block_normal = ("J_block_norm.gif")
j_block_180 = ("J_block_180.gif")
j_block_right = ("J_block_right.gif")
j_block_left = ("J_block_left.gif")
wn.addshape (square)
wn.addshape(s_block_normal)
wn.addshape(s_block_standing)
wn.addshape(invert_s_block_normal)
wn.addshape(invert_s_block_standing)
wn.addshape(l_block_normal)
wn.addshape(l_block_180)
wn.addshape(l_block_right)
wn.addshape(l_block_left)
wn.addshape(line_normal)
wn.addshape(line_standing)
wn.addshape(t_block_normal)
wn.addshape(t_block_180)
wn.addshape(t_block_right)
wn.addshape(t_block_left)
wn.addshape(j_block_normal)
wn.addshape(j_block_180)
wn.addshape(j_block_right)
wn.addshape(j_block_left)
Tshape = [square, s_block_normal, invert_s_block_normal, l_block_normal, line_normal, t_block_normal, j_block_normal]
squarecor = [-50, -25, -100, -50]
wn.bgcolor("lightblue")
background.speed("fastest")
background.hideturtle()
background.pu()
background.goto(-400,-200)
background.fillcolor("blue")
background.pencolor("blue")
background.begin_fill()
background.pd()
background.goto(400,-200)
background.goto(400,-350)
background.goto(-400,-350)
background.goto(-400,-200)
background.end_fill()
#sprite direction change
def sprite_right():
global tetris
tetris.hideturtle()
if (tetris.shape(s_block_normal)):
tetris.shape(s_block_standing)
elif (tetris.shape(invert_s_block_normal)):
tetris.shape(invert_s_block_standing)
elif (tetris.shape(line_normal)):
tetris.shape(line_standing)
elif (tetris.shape(l_block_normal)):
tetris.shape(l_block_right)
elif (tetris.shape(t_block_normal)):
tetris.shape(t_block_right)
elif (tetris.shape(j_block_normal)):
tetris.shape(j_block_right)
elif (tetris.shape(l_block_right)):
tetris.shape(l_block_180)
elif (tetris.shape(t_block_right)):
tetris.shape(t_block_180)
elif (tetris.shape(j_block_right)):
tetris.shape(j_block_180)
elif (tetris.shape(s_block_standing)):
tetris.shape(s_block_normal)
elif (tetris.shape(invert_s_block_standing)):
tetris.shape(invert_s_block_normal)
elif (tetris.shape(line_standing)):
tetris.shape(line_normal)
elif (tetris.shape(l_block_180)):
tetris.shape(l_block_left)
elif (tetris.shape(t_block_180)):
tetris.shape(t_block_left)
elif (tetris.shape(j_block_180)):
tetris.shape(j_block_left)
elif (tetris.shape(l_block_left)):
tetris.shape(l_block_normal)
elif (tetris.shape(t_block_left)):
tetris.shape(t_block_normal)
elif (tetris.shape(j_block_left)):
tetris.shape(j_block_normal)
tetris.showturtle()
def sprite_left():
tetris.hideturtle()
if (tetris.shape(s_block_normal)):
tetris.shape(s_block_standing)
elif (tetris.shape(invert_s_block_normal)):
tetris.shape(invert_s_block_standing)
elif (tetris.shape(line_normal)):
tetris.shape(line_standing)
elif (tetris.shape(l_block_normal)):
tetris.shape(l_block_left)
elif (tetris.shape(t_block_normal)):
tetris.shape(t_block_left)
elif (tetris.shape(j_block_normal)):
tetris.shape(j_block_left)
elif (tetris.shape(l_block_left)):
tetris.shape(l_block_180)
elif (tetris.shape(t_block_left)):
tetris.shape(t_block_180)
elif (tetris.shape(j_block_left)):
tetris.shape(j_block_180)
elif (tetris.shape(s_block_standing)):
tetris.shape(s_block_normal)
elif (tetris.shape(invert_s_block_standing)):
tetris.shape(invert_s_block_normal)
elif (tetris.shape(line_standing)):
tetris.shape(line_normal)
elif (tetris.shape(l_block_180)):
tetris.shape(l_block_right)
elif (tetris.shape(t_block_180)):
tetris.shape(t_block_right)
elif (tetris.shape(j_block_180)):
tetris.shape(j_block_right)
elif (tetris.shape(l_block_right)):
tetris.shape(l_block_normal)
elif (tetris.shape(t_block_right)):
tetris.shape(t_block_normal)
elif (tetris.shape(j_block_right)):
tetris.shape(j_block_normal)
tetris.showturtle()
'''
def (fast_down):
global tetris
tetris.setheading(270)
tetris.forward(10)
'''
#turn right
def turn_right():
tetris.speed("fastest")
tetris.setheading(0)
tetris.speed(1)
tetris.forward(10)
tetris.speed("fastest")
tetris.setheading(270)
#turn left
def turn_left():
tetris.speed("fastest")
tetris.setheading(180)
tetris.speed(1)
tetris.forward(10)
tetris.speed("fastest")
tetris.setheading(270)
#down
#tetris container/backround
drawer.pensize(5)
drawer.speed("fastest")
drawer.penup()
drawer.goto(150, 200)
drawer.pendown()
drawer.setheading(270)
drawer.forward(400)
drawer.setheading(180)
drawer.forward(300)
drawer.setheading(90)
drawer.forward(400)
drawer.hideturtle()
#game WIP!!!!!!!!!!
y = 0
space = int(1)
tracer = True
def spawn_T():
new_T=rand.choice(Tshape)
tetris.shape(t_block_180)
tetris.pu()
tetris.goto(0, 200)
tetris.setheading(270)
tetris.forward(10)
'''
if ((abs(space - starty)) < 200):
tetris.forward(2)
else:
'''
tetris.stamp()
'''
space = space =+ 1
'''
tetris.hideturtle()
tetris.goto(0,200)
tetris.showturtle()
if (y == 0):
spawn_T()
#changeing the directions of the sprites
#events
wn.onkeypress(turn_right, "Right")
wn.onkeypress(turn_left, "Left")
wn.onkeypress(sprite_right, "a")
wn.onkeypress(sprite_left, "d")
'''
wn.onkeypress(fast_down, "s")
'''
wn.listen()
wn.mainloop()
Here is the google drive file https://drive.google.com/drive/folders/1Q_zXEqm4aFHQ4RV-ZvEXCK2Wi7SHMxrW?usp=share_link
I think the indentation is wrong here. Maybe try putting the last else-statement one tab further?
Please use elif instead of the else: if formulation to get away from the bewildering indentation - here's how it should look:
def sprite_right():
global tetris
tetris.hideturtle()
if (tetris.shape(s_block_normal)):
tetris.shape(s_block_standing)
elif (tetris.shape(invert_s_block_normal)):
tetris.shape(invert_s_block_standing)
elif (tetris.shape(line_normal)):
tetris.shape(line_standing)
elif (tetris.shape(l_block_normal)):
tetris.shape(l_block_right)
elif (tetris.shape(t_block_normal)):
tetris.shape(t_block_right)
elif (tetris.shape(j_block_normal)):
tetris.shape(j_block_right)
elif (tetris.shape(l_block_right)):
tetris.shape(l_block_180)
elif (tetris.shape(t_block_right)):
tetris.shape(t_block_180)
elif (tetris.shape(j_block_right)):
tetris.shape(j_block_180)
elif (tetris.shape(s_block_standing)):
tetris.shape(s_block_normal)
elif (tetris.shape(invert_s_block_standing)):
tetris.shape(invert_s_block_normal)
elif (tetris.shape(line_standing)):
tetris.shape(line_normal)
elif (tetris.shape(l_block_180)):
tetris.shape(l_block_left)
elif (tetris.shape(t_block_180)):
tetris.shape(t_block_left)
elif (tetris.shape(j_block_180)):
tetris.shape(j_block_left)
elif (tetris.shape(l_block_left)):
tetris.shape(l_block_normal)
elif (tetris.shape(t_block_left)):
tetris.shape(t_block_normal)
elif (tetris.shape(j_block_left)):
tetris.shape(j_block_normal)
tetris.showturtle()
def sprite_left():
tetris.hideturtle()
if (tetris.shape(s_block_normal)):
tetris.shape(s_block_standing)
elif (tetris.shape(invert_s_block_normal)):
tetris.shape(invert_s_block_standing)
elif (tetris.shape(line_normal)):
tetris.shape(line_standing)
elif (tetris.shape(l_block_normal)):
tetris.shape(l_block_left)
elif (tetris.shape(t_block_normal)):
tetris.shape(t_block_left)
elif (tetris.shape(j_block_normal)):
tetris.shape(j_block_left)
elif (tetris.shape(l_block_left)):
tetris.shape(l_block_180)
elif (tetris.shape(t_block_left)):
tetris.shape(t_block_180)
elif (tetris.shape(j_block_left)):
tetris.shape(j_block_180)
elif (tetris.shape(s_block_standing)):
tetris.shape(s_block_normal)
elif (tetris.shape(invert_s_block_standing)):
tetris.shape(invert_s_block_normal)
elif (tetris.shape(line_standing)):
tetris.shape(line_normal)
elif (tetris.shape(l_block_180)):
tetris.shape(l_block_right)
elif (tetris.shape(t_block_180)):
tetris.shape(t_block_right)
elif (tetris.shape(j_block_180)):
tetris.shape(j_block_right)
elif (tetris.shape(l_block_right)):
tetris.shape(l_block_normal)
elif (tetris.shape(t_block_right)):
tetris.shape(t_block_normal)
elif (tetris.shape(j_block_right)):
tetris.shape(j_block_normal)
tetris.showturtle()
That said, because this is all the code you've provided, it's not possible to run it and examine why it's not working. Can you please provide an example of an implementation of these functions?
According to the documentation, shape() returns the current shape with no parameters, and sets it with one parameter. Therefore in:
if tetris.shape(s_block_normal):
shape(name) returns None, which is always False. So, if you change to:
if tetris.shape() == s_block_normal:
It should function as required.
Also, you can get rid of the if ladder completely by, for instance, something like:
next_left_shape = {s_block_normal: s_block_standing,
invert_s_block_normal: invert_s_block_standing,
... }
tetris.shape(next_left_shape[tetris.shape()])
This question already has answers here:
How do I use a C-style for loop in Python?
(8 answers)
Closed 3 years ago.
I am trying to change the index of a for loop depending if a user wants to go to the previous image (index = index - 1) and if he wants to go the next image (index = index + 1) however, the index doesnt change in the outerloop (outside the if statements).
flag = False
while flag == False:
for i in range(len(all_image)):
image = all_image[i]
print(i)
userchoice = easygui.buttonbox(msg, image = image, choices=choices)
if userchoice == 'Next':
i = i+1
elif userchoice == 'Previous':
i = i-1
elif userchoice == 'cancel':
print('test')
flag = True
break
Thanks in advance.
Not sure why the for loop is there, I'd be tempted to structure it more like so:
flag = False
idx = 0 #idx is where we are in the image list, and it gets modified by use choices in the while loop.
while flag == False:
image = all_image[idx]
print(idx)
userchoice = easygui.buttonbox(msg, image = image, choices=choices)
if userchoice == 'Next':
idx += 1
elif userchoice == 'Previous':
idx -= 1
elif userchoice == 'cancel':
print('test')
flag = True
break
I am new to coding and has recently started learning python. My first challenge is to build a tic tac toe game. Below are my codes for the game, everything worked fine except that when there is no winner(i.e. game draw); i want to display no body has won the game. I've tried to incorporate an else in various places. but, they didn't help. Here is my code.
# a dictionary to display the grid
grid = {
'1':'1', '2':'2', '3':'3',
'4':'4', '5':'5', '6':'6',
'7':'7', '8':'8', '9':'9'
}
# a list to store the values that has already been entered
used_places = []
# players and their symbols
player_1 = 'x'
player_2 = 'o'
# to store result
result = None
def grid_display():
#this function displays the grid
print('\n\n')
print(grid['1'], '\t|\t', grid['2'], '\t|\t', grid['3'])
print(''.rjust(19,'*'))
print(grid['4'], '\t|\t', grid['5'], '\t|\t', grid['6'])
print(''.rjust(19, '*'))
print(grid['7'], '\t|\t', grid['8'], '\t|\t', grid['9'])
def win_the_game():
# this function checks for result and returns true or false
if(
(grid['1'] == grid['2'] == grid['3']) or (grid['4'] == grid['5'] == grid['6']) or
(grid['7'] == grid['8'] == grid['9']) or (grid['1'] == grid['4'] == grid['7']) or
(grid['2'] == grid['5'] == grid['8']) or (grid['3'] == grid['6'] == grid['9']) or
(grid['1'] == grid['5'] == grid['9']) or (grid['3'] == grid['5'] == grid['7'])
):
return True
else:
return False
def taking_turns(turn):
#this function asks user for input
print("its turn of ", turn)
choice = input("enter your choice")
while not (choice.isdecimal() and choice in grid and (choice not in used_places)):
# taking proper input by checkin it with already entered numbers
# and if it is a number
# and if number is in between 1 and 9
choice = input("\nenter your choice properly:")
player_move(choice, turn)
def player_move(move, assign):
# this function fills the entered numbers into used_places list
# and replaces numbers in grid with user input
used_places.append(move)
grid[move] = assign
print("player 1 : 'X'")
print("player 2 : 'O'")
for i in range(0,10): # loops 9 times to play the game
if i % 2 == 0: # giving turns. if i is even, player 1 gets turn and odd, player 2
grid_display() # displaying complete grid
turn = player_1 # to display whose turn it is; refer the function
taking_turns(turn)
if win_the_game() == True: # if the called function returns true in this 'if'
result = turn # player 1 wins
break
else:
grid_display() # same code as above
turn = player_2
taking_turns(turn)
if win_the_game() == True:
result = turn
break
print('\n\n',result, "won the game!!") # printing result
Instead of setting result = None, set result = 'Nobody'.
Looking at your logic, you only set result if somebody wins, this would leave the default of result to nobody.
-- edit --
Sorry, I kinda got carried away and re-wrote the logic of your game in proving my solution. Take it or leave it, but it works great now and maybe you can use it as an example to fix your own.
import os
# a dictionary to display the grid
grid = {
'1':'1', '2':'2', '3':'3',
'4':'4', '5':'5', '6':'6',
'7':'7', '8':'8', '9':'9'
}
# a list to store the values that are available
places = ['1','2','3','4','5','6','7','8','9']
# to store result
result = 'Nobody'
# starting player
turn = 'O'
# display a game grid
def grid_display():
os.system('cls' if os.name == 'nt' else 'clear')
#this function displays the grid
print
print(grid['1'], '|', grid['2'], '|', grid['3'])
print(''.rjust(25,'*'))
print(grid['4'], '|', grid['5'], '|', grid['6'])
print(''.rjust(25, '*'))
print(grid['7'], '|', grid['8'], '|', grid['9'])
# check to see if anyone has won or are we out of moves
def win_the_game():
# this function checks for result and returns true or false
if(
(grid['1'] == grid['2'] == grid['3']) or (grid['4'] == grid['5'] == grid['6']) or
(grid['7'] == grid['8'] == grid['9']) or (grid['1'] == grid['4'] == grid['7']) or
(grid['2'] == grid['5'] == grid['8']) or (grid['3'] == grid['6'] == grid['9']) or
(grid['1'] == grid['5'] == grid['9']) or (grid['3'] == grid['5'] == grid['7'])
):
return True
# checks if there are any moves left
elif not places:
return False
else:
return False
# input / grid update function
def taking_turns():
# this function asks user for input
# use RAW_INPUT to make it a string
print
print map(str, places)
choice = raw_input("\nEnter "+turn+"'s choice: ")
if choice in places:
# this removes the number from the available list
# and replaces numbers in grid with user input
places.remove(choice)
grid[choice] = turn
# Logic loop
while places:
grid_display() # always display the grid
if turn == "O": # giving turns.
turn = 'X'
taking_turns()
if win_the_game() == True: # if the called function returns true in this 'if'
result = turn # player 1 wins
grid_display() # Winners GRID
break
else:
turn = 'O'
taking_turns()
if win_the_game() == True:
result = turn
grid_display() # Winners GRID
break
# results
print
print(result, "won the game!!") # printing result
print
I have a problem. I want to create a form in python. If something is wrong, I want an alert-window (showwarning()). Otherwise it should write 'TRUE' into the command line.
The problem is that I get every time a alert-window. It does not care if the form is filled out correctly or wrong.
Can somebody help me with this problem?
code:
""" Variables """
inputError_1 = bool(0)
inputError_2 = bool(0)
inputError_3 = bool(0)
valueCheck = bool(0)
""" Check-Button """
def Check():
if len(nameOne.get()) == 0:
inputError_1 == TRUE
elif len(nameTwo.get()) == 0:
inputError_2 == TRUE
elif len(comment.get(INSERT)) == 0:
inputError_3 == TRUE
else:
valueCheck = bool(1)
if inputError_1 == FALSE or inputError_2 == FALSE or inputError_3 == FALSE:
showwarning()
else:
print'TRUE'
I think that you can do this in a simpler way:
def check():
if len(nameOne.get()) == 0 or len(nameTwo.get()) == 0 or len(comment.get(INSERT)) == 0:
showwarning()
else:
print 'True'
check()
def menurender():
global pos
global menulist
line1=menulist[pos]
if pos == len(menulist):
line2="back"
else:
line2=menulist[pos+1]
lcd.clear()
lcd.message(str(pos)+' ' +line1+ "\n"+str(pos+1)+' '+ line2)
In my block of code, I have a conditional in the menurender() function that checks to make sure that the list menulist has a valid index before referencing it, but i receive IndexError: list index out of range. I understand that the else statement is causing it, but I am confused because python shouldn't be executing it.
Full code
#!/usr/bin/python
#################################################
#IMPORTS#########################################
#################################################
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from Adafruit_I2C import Adafruit_I2C
#################################################
#OBJECTS#########################################
#################################################
lcd = Adafruit_CharLCDPlate()
#################################################
#VARIABLES#######################################
#################################################
#current button value
prevbutton = "NULL"
#for SELECT key and determining clicks
action = False
#variable for menu position
pos = 0
#on screen cursor 0 for top line, 1 for bottom line
cursor = 0
#Handles list structure and action when clicked
menulist= []
menulist.append("CPU")
menulist.append("RAM")
menulist.append("STORAGE")
menulist.append("NETWORK")
#get input from keys and return the currently pressed key
def buttonstatus():
bstatus = "Null"
if lcd.buttonPressed(lcd.SELECT) == True:
bstatus="SELECT"
elif lcd.buttonPressed(lcd.UP) == True:
bstatus="UP"
elif lcd.buttonPressed(lcd.DOWN) == True:
bstatus="DOWN"
elif lcd.buttonPressed(lcd.LEFT) == True:
bstatus="LEFT"
elif lcd.buttonPressed(lcd.RIGHT) == True:
bstatus="RIGHT"
return bstatus
#checks buttons pressed and converts that into action for top menu
def getinput():
global prevbutton
global pos
if buttonstatus() != prevbutton:
prevbutton = buttonstatus()
if buttonstatus() == "SELECT":
print "select"
elif buttonstatus() == "DOWN":
pos = pos + 1
elif buttonstatus() == "UP":
pos = pos -1
#elif buttonstatus() == "LEFT":
#print "left"
#elif buttonstatus() == "RIGHT":
#print "right"
#defines bounds for the position of the cursor
def posbounds():
global pos
global menulist
if pos < 0:
pos = 0
if pos == len(menulist):
pos = len(menulist)
#code renders the menu on the LCD
def menurender():
global pos
global menulist
line1=menulist[pos]
if pos == len(menulist):
line2="back"
else:
line2=menulist[pos+1]
lcd.clear()
lcd.message(str(pos)+' ' +line1+ "\n"+str(pos+1)+' '+ line2)
while True:
getinput()
posbounds()
menurender()
There are lots if values for pos != len(menulist) for which menulist[pos+1] gives an IndexError (including pos == len(menulist) - 1). You should check
if pos > (len(menulist) - 2):
You should probably change if pos == len(menulist): to if pos == len(menulist) - 1: if you want to check if pos is the index of the last element, or to if pos == len(menulist) - 2: if you want to check for the second to last element.
A better way of doing this may be by using a try ... except block.
try:
line2 = menulist[pos+1]
except IndexError:
# Out of range -> pos is greater than len(menulist)-1
line2 = 'back'
However - this doesn't seem lika a Pythonic way of doing anything at all in Python. Maybe you could tell us what you are trying to achieve, and someone here may propose a better way of doing it.