'PyQt6 Button to choose random string and update a label with the new string?
I'm trying to make the game "word on the street" to play on a computer (for myself). (For some reason it doesn't exist electronically). How would I go about using PyQt6 to make a button that, upon being pressed, will pick a random string from a list and then update the text within a label as that string? I'm a beginner, so I expect this is a stupid question.
I haven't yet gotten around to implementing the letters and them moving around, the points system, or the idea of a turn, so I don't have any specific questions regarding those. However, if anyone wants to give some suggestions for that, it would be much appreciated. Thanks!
import sys
import random
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
Cards_List = ["A kind of Make-up","A String Instrument","Something Found in the Arctic","An object made to be held in one hand","The last name of a child star","the name of a bone","A City With A population Of Over One Million","Something found in a tomb","an element from the periodic table","a shakespearean character","a brass instrument","a college or university","something made of plastic","something carried or worn by soldiers","a famous dog","an animal by-product","something that can be knitted","a political party","a kind of paint","part of a helicopter","a type of knife","something that melts in the sun","a kind of book","a band","something that pregnant woman should not do","a building material","something depressing","something with a bell","something made of rubber or latex","a card game","a country that produces oil","a type of milk","a salad","a mathematical term","something found on the bottom of the ocean","a type of energy or power","a country in south america","something worn by a bride","a kind of tea","a \"hot button issue\"","something dangerous","a kind of paper","something romantic","a country that has hosted the olympics","a predatory animal","a scientific instrument","a tropical destination","the name of a street","a candle scent","information found in a passport","something people blow on, in, or up","something that bounces","something with a shell","a title used for males but not for females","an internal organ","something that emits heat","a computer program","a health food","a word that describes a car crash","something that can be done with an onion","a bridge","something used to navigate","the abbreviation for an organization or agency","a way to make money","something operated with both hands","something that is taxed","a type of bottle","a type of classical music composition","a unit of measurement","something used by scuba divers","a classical composer","something done with the nose","something that comes in a bunch","a primate","a political office","a non-coastal city","a city in canada","something carried or worn by a pirate","something that comes on a roll","a type of pasta","a salad dressing","an infection","something worn or used while hunting","a department in a business","a country in africa","a \"pet name\" for a loved one", "a cooking method","a perfume or cologne scent","a famous puppet or doll","a film genre","an invertebrate animal","a virtue or one of the seven deadly sins","a product used for cleaning","the last name of a famous african american","the last name of a famous jew","the last name of a famous rabbi","a shape or symbol found on a flag","a kind of rock or stone","a measuring tool or device","something purchased for a baby","a genre of music","a famous horse","a city where one of the players has lived","a mountain","a dance","the last name of a comedian","an ancient civilization","something slimy or slippery","something the ancient egyptians made or used","a domesticated animal","a video game","something transparent or translucent","something distinctively american","a type of pepper","something that stretches","a renewable energy source","something people put in their hair","a players\'s occupation","a dog breed","something toxic or poisonous","a beach","a type of cheese","a root or tuber","something that can cause a headache","a search engine or social networking site","something people use to see better","a disease","something that comes in a pack","a military rank","a type of rice or a food made with rice","a condiment","something found in a bank","something sold on the black market","a type of tape","a brand of marker, pen or pencil","another word for sad","something found on a map","something cylindrical","a kind of coffee or a coffee drink","something waxed or made with wax","something used by golfers","a mushroom","something moset people will never see","a word that ends in \"ism\"","a nocturnal animal","an event where a speech is made","something that comes in gallons or liters","a martial art or combat sport","a breakfast cereal","a kind of grass","something outside of earth's atmosphere","an island","part of a parade"," something that can be done with money"," a religious ceremony","a method of preparing vegetables","a name in the bible, torah or koran","an angel, saint, devil or demon","somethin with a string or rope","a type of tower","a key on a computer keyboard","something that is often framed","the title of a play or musical","a sauce","a hotel or motel","a greek or roman god or goddess","something with a string","something that is wasted","something viscous","a zodiac sign or constellation","a leafy green food","something found in a hospital","something studied in geometry","human-powered transportation","a type of advertisement","something microscopic","the last name of a talk show host","the last name of a famous artist","something sold on the street","another word for happy","something that crunches","an airline","a reason to get married","a type of currency","a type of metal","a title used for females but not for males","a classical composer","a way to communicate","a religion","something used for security or self-defense","an illegal activity","a halloween decoration","a villain or monster","something that can be rolled up","a deodorant or soap brand","something that can be locked","a percussion instrument","something that comes in a tube","a country in Asia","a herd animal","the capital of a U.S. state","the capital of a country","a grain or flour","a letter in a foreign alphabet","an author's last name","an edible seed","a mineral or gem","a mythological animal","something used to keep time","a hairstyle","a woodwind instrument","a string instrument","a brass instrument","the brand of clothing word by one of the players","something found at a wedding","a criminal offense","a place to invest money","a school attended by one of the players","the last name of a foreign leader","a type of coat or jacket","part of a boat or ship","a material used in construction","a computer component","a material used to make clothing","a type of ground cover","one of the books of the old testament","a book of the talmud",]
app = QApplication(sys.argv)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("Word on the Street")
self.setStyleSheet("background-color: #D7FDF5;")
self.button = QPushButton("New Turn")
self.button.clicked.connect(self.the_button_was_clicked)
Current_Card = QLabel(Card, self)
Player1 = QLabel("Player 1", self)
Player2 = QLabel("Player 2", self)
Player1score = QLabel(self)
Player2score = QLabel(self)
b0 = QLabel(self)
b1 = QLabel(self)
b2 = QLabel(self)
b3 = QLabel(self)
b4 = QLabel(self)
b5 = QLabel(self)
b6 = QLabel(self)
c0 = QLabel(self)
c1 = QLabel(self)
c2 = QLabel(self)
c3 = QLabel(self)
c4 = QLabel(self)
c5 = QLabel(self)
c6 = QLabel(self)
d0 = QLabel(self)
d1 = QLabel(self)
d2 = QLabel(self)
d3 = QLabel(self)
d4 = QLabel(self)
d5 = QLabel(self)
d6 = QLabel(self)
f0 = QLabel(self)
f1 = QLabel(self)
f2 = QLabel(self)
f3 = QLabel(self)
f4 = QLabel(self)
f5 = QLabel(self)
f6 = QLabel(self)
g0 = QLabel(self)
g1 = QLabel(self)
g2 = QLabel(self)
g3 = QLabel(self)
g4 = QLabel(self)
g5 = QLabel(self)
g6 = QLabel(self)
h0 = QLabel(self)
h1 = QLabel(self)
h2 = QLabel(self)
h3 = QLabel(self)
h4 = QLabel(self)
h5 = QLabel(self)
h6 = QLabel(self)
k0 = QLabel(self)
k1 = QLabel(self)
k2 = QLabel(self)
k3 = QLabel(self)
k4 = QLabel(self)
k5 = QLabel(self)
k6 = QLabel(self)
l0 = QLabel(self)
l1 = QLabel(self)
l2 = QLabel(self)
l3 = QLabel(self)
l4 = QLabel(self)
l5 = QLabel(self)
l6 = QLabel(self)
m0 = QLabel(self)
m1 = QLabel(self)
m2 = QLabel(self)
m3 = QLabel(self)
m4 = QLabel(self)
m5 = QLabel(self)
m6 = QLabel(self)
n0 = QLabel(self)
n1 = QLabel(self)
n2 = QLabel(self)
n3 = QLabel(self)
n4 = QLabel(self)
n5 = QLabel(self)
n6 = QLabel(self)
p0 = QLabel(self)
p1 = QLabel(self)
p2 = QLabel(self)
p3 = QLabel(self)
p4 = QLabel(self)
p5 = QLabel(self)
p6 = QLabel(self)
r0 = QLabel(self)
r1 = QLabel(self)
r2 = QLabel(self)
r3 = QLabel(self)
r4 = QLabel(self)
r5 = QLabel(self)
r6 = QLabel(self)
s0 = QLabel(self)
s1 = QLabel(self)
s2 = QLabel(self)
s3 = QLabel(self)
s4 = QLabel(self)
s5 = QLabel(self)
s6 = QLabel(self)
t0 = QLabel(self)
t1 = QLabel(self)
t2 = QLabel(self)
t3 = QLabel(self)
t4 = QLabel(self)
t5 = QLabel(self)
t6 = QLabel(self)
v0 = QLabel(self)
v1 = QLabel(self)
v2 = QLabel(self)
v3 = QLabel(self)
v4 = QLabel(self)
v5 = QLabel(self)
v6 = QLabel(self)
w0 = QLabel(self)
w1 = QLabel(self)
w2 = QLabel(self)
w3 = QLabel(self)
w4 = QLabel(self)
w5 = QLabel(self)
w6 = QLabel(self)
y0 = QLabel(self)
y1 = QLabel(self)
y2 = QLabel(self)
y3 = QLabel(self)
y4 = QLabel(self)
y5 = QLabel(self)
y6 = QLabel(self)
Current_Card.setStyleSheet("border: 1px solid black")
self.button.setStyleSheet("border: 1px solid black")
Player1.setStyleSheet("border: 1px solid black;")
Player2.setStyleSheet("border: 1px solid black;")
Player1score.setStyleSheet("border: 1px solid black;")
Player2score.setStyleSheet("border: 1px solid black;")
b0.setStyleSheet("border: 1px solid black")
b1.setStyleSheet("border: 1px solid black;")
b2.setStyleSheet("border: 1px solid black;")
b3.setStyleSheet("border: 1px solid black;")
b4.setStyleSheet("border: 1px solid black;")
b5.setStyleSheet("border: 1px solid black;")
b6.setStyleSheet("border: 1px solid black;")
c0.setStyleSheet("border: 1px solid black;")
c1.setStyleSheet("border: 1px solid black;")
c2.setStyleSheet("border: 1px solid black;")
c3.setStyleSheet("border: 1px solid black;")
c4.setStyleSheet("border: 1px solid black;")
c5.setStyleSheet("border: 1px solid black;")
c6.setStyleSheet("border: 1px solid black;")
d0.setStyleSheet("border: 1px solid black;")
d1.setStyleSheet("border: 1px solid black;")
d2.setStyleSheet("border: 1px solid black;")
d3.setStyleSheet("border: 1px solid black;")
d4.setStyleSheet("border: 1px solid black;")
d5.setStyleSheet("border: 1px solid black;")
d6.setStyleSheet("border: 1px solid black;")
f0.setStyleSheet("border: 1px solid black;")
f1.setStyleSheet("border: 1px solid black;")
f2.setStyleSheet("border: 1px solid black;")
f3.setStyleSheet("border: 1px solid black;")
f4.setStyleSheet("border: 1px solid black;")
f5.setStyleSheet("border: 1px solid black;")
f6.setStyleSheet("border: 1px solid black;")
g0.setStyleSheet("border: 1px solid black;")
g1.setStyleSheet("border: 1px solid black;")
g2.setStyleSheet("border: 1px solid black;")
g3.setStyleSheet("border: 1px solid black;")
g4.setStyleSheet("border: 1px solid black;")
g5.setStyleSheet("border: 1px solid black;")
g6.setStyleSheet("border: 1px solid black;")
h0.setStyleSheet("border: 1px solid black;")
h1.setStyleSheet("border: 1px solid black;")
h2.setStyleSheet("border: 1px solid black;")
h3.setStyleSheet("border: 1px solid black;")
h4.setStyleSheet("border: 1px solid black;")
h5.setStyleSheet("border: 1px solid black;")
h6.setStyleSheet("border: 1px solid black;")
k0.setStyleSheet("border: 1px solid black;")
k1.setStyleSheet("border: 1px solid black;")
k2.setStyleSheet("border: 1px solid black;")
k3.setStyleSheet("border: 1px solid black;")
k4.setStyleSheet("border: 1px solid black;")
k5.setStyleSheet("border: 1px solid black;")
k6.setStyleSheet("border: 1px solid black;")
l0.setStyleSheet("border: 1px solid black;")
l1.setStyleSheet("border: 1px solid black;")
l2.setStyleSheet("border: 1px solid black;")
l3.setStyleSheet("border: 1px solid black;")
l4.setStyleSheet("border: 1px solid black;")
l5.setStyleSheet("border: 1px solid black;")
l6.setStyleSheet("border: 1px solid black;")
m0.setStyleSheet("border: 1px solid black;")
m1.setStyleSheet("border: 1px solid black;")
m2.setStyleSheet("border: 1px solid black;")
m3.setStyleSheet("border: 1px solid black;")
m4.setStyleSheet("border: 1px solid black;")
m5.setStyleSheet("border: 1px solid black;")
m6.setStyleSheet("border: 1px solid black;")
n0.setStyleSheet("border: 1px solid black;")
n1.setStyleSheet("border: 1px solid black;")
n2.setStyleSheet("border: 1px solid black;")
n3.setStyleSheet("border: 1px solid black;")
n4.setStyleSheet("border: 1px solid black;")
n5.setStyleSheet("border: 1px solid black;")
n6.setStyleSheet("border: 1px solid black;")
p0.setStyleSheet("border: 1px solid black;")
p1.setStyleSheet("border: 1px solid black;")
p2.setStyleSheet("border: 1px solid black;")
p3.setStyleSheet("border: 1px solid black;")
p4.setStyleSheet("border: 1px solid black;")
p5.setStyleSheet("border: 1px solid black;")
p6.setStyleSheet("border: 1px solid black;")
r0.setStyleSheet("border: 1px solid black;")
r1.setStyleSheet("border: 1px solid black;")
r2.setStyleSheet("border: 1px solid black;")
r3.setStyleSheet("border: 1px solid black;")
r4.setStyleSheet("border: 1px solid black;")
r5.setStyleSheet("border: 1px solid black;")
r6.setStyleSheet("border: 1px solid black;")
s0.setStyleSheet("border: 1px solid black;")
s1.setStyleSheet("border: 1px solid black;")
s2.setStyleSheet("border: 1px solid black;")
s3.setStyleSheet("border: 1px solid black;")
s4.setStyleSheet("border: 1px solid black;")
s5.setStyleSheet("border: 1px solid black;")
s6.setStyleSheet("border: 1px solid black;")
t0.setStyleSheet("border: 1px solid black;")
t1.setStyleSheet("border: 1px solid black;")
t2.setStyleSheet("border: 1px solid black;")
t3.setStyleSheet("border: 1px solid black;")
t4.setStyleSheet("border: 1px solid black;")
t5.setStyleSheet("border: 1px solid black;")
t6.setStyleSheet("border: 1px solid black;")
v0.setStyleSheet("border: 1px solid black;")
v1.setStyleSheet("border: 1px solid black;")
v2.setStyleSheet("border: 1px solid black;")
v3.setStyleSheet("border: 1px solid black;")
v4.setStyleSheet("border: 1px solid black;")
v5.setStyleSheet("border: 1px solid black;")
v6.setStyleSheet("border: 1px solid black;")
w0.setStyleSheet("border: 1px solid black;")
w1.setStyleSheet("border: 1px solid black;")
w2.setStyleSheet("border: 1px solid black;")
w3.setStyleSheet("border: 1px solid black;")
w4.setStyleSheet("border: 1px solid black;")
w5.setStyleSheet("border: 1px solid black;")
w6.setStyleSheet("border: 1px solid black;")
y0.setStyleSheet("border: 1px solid black;")
y1.setStyleSheet("border: 1px solid black;")
y2.setStyleSheet("border: 1px solid black;")
y3.setStyleSheet("border: 1px solid black;")
y4.setStyleSheet("border: 1px solid black;")
y5.setStyleSheet("border: 1px solid black;")
y6.setStyleSheet("border: 1px solid black;")
grid = QGridLayout()
grid.setSpacing(0)
grid.addWidget(self.button, 6, 0)
grid.addWidget(Current_Card, 5, 0)
grid.addWidget(Player1, 0, 0)
grid.addWidget(Player2, 0, 8)
grid.addWidget(Player1score, 1, 0)
grid.addWidget(Player2score, 1, 8)
grid.addWidget(b0, 0, 1)
grid.addWidget(b1, 0,2)
grid.addWidget(b2, 0,3)
grid.addWidget(b3, 0,4)
grid.addWidget(b4, 0,5)
grid.addWidget(b5, 0,6)
grid.addWidget(b6, 0,7)
grid.addWidget(c0, 1,1)
grid.addWidget(c1, 1,2)
grid.addWidget(c2, 1,3)
grid.addWidget(c3, 1,4)
grid.addWidget(c4, 1,5)
grid.addWidget(c5, 1,6)
grid.addWidget(c6, 1,7)
grid.addWidget(d0, 2,1)
grid.addWidget(d1, 2,2)
grid.addWidget(d2, 2,3)
grid.addWidget(d3, 2,4)
grid.addWidget(d4, 2,5)
grid.addWidget(d5, 2,6)
grid.addWidget(d6, 2,7)
grid.addWidget(f0, 3,1)
grid.addWidget(f1, 3,2)
grid.addWidget(f2, 3,3)
grid.addWidget(f3, 3,4)
grid.addWidget(f4, 3,5)
grid.addWidget(f5, 3,6)
grid.addWidget(f6, 3,7)
grid.addWidget(g0, 4,1)
grid.addWidget(g1, 4,2)
grid.addWidget(g2, 4,3)
grid.addWidget(g3, 4,4)
grid.addWidget(g4, 4,5)
grid.addWidget(g5, 4,6)
grid.addWidget(g6, 4,7)
grid.addWidget(h0, 5,1)
grid.addWidget(h1, 5,2)
grid.addWidget(h2, 5,3)
grid.addWidget(h3, 5,4)
grid.addWidget(h4, 5,5)
grid.addWidget(h5, 5,6)
grid.addWidget(h6, 5,7)
grid.addWidget(k0, 6,1)
grid.addWidget(k1, 6,2)
grid.addWidget(k2, 6,3)
grid.addWidget(k3, 6,4)
grid.addWidget(k4, 6,5)
grid.addWidget(k5, 6,6)
grid.addWidget(k6, 6,7)
grid.addWidget(l0, 7,1)
grid.addWidget(l1, 7,2)
grid.addWidget(l2, 7,3)
grid.addWidget(l3, 7,4)
grid.addWidget(l4, 7,5)
grid.addWidget(l5, 7,6)
grid.addWidget(l6, 7,7)
grid.addWidget(m0, 8,1)
grid.addWidget(m1, 8,2)
grid.addWidget(m2, 8,3)
grid.addWidget(m3, 8,4)
grid.addWidget(m4, 8,5)
grid.addWidget(m5, 8,6)
grid.addWidget(m6, 8,7)
grid.addWidget(n0, 9,1)
grid.addWidget(n1, 9,2)
grid.addWidget(n2, 9,3)
grid.addWidget(n3, 9,4)
grid.addWidget(n4, 9,5)
grid.addWidget(n5, 9,6)
grid.addWidget(n6, 9,7)
grid.addWidget(p0, 10,1)
grid.addWidget(p1, 10,2)
grid.addWidget(p2, 10,3)
grid.addWidget(p3, 10,4)
grid.addWidget(p4, 10,5)
grid.addWidget(p5, 10,6)
grid.addWidget(p6, 10,7)
grid.addWidget(r0, 11,1)
grid.addWidget(r1, 11,2)
grid.addWidget(r2, 11,3)
grid.addWidget(r3, 11,4)
grid.addWidget(r4, 11,5)
grid.addWidget(r5, 11,6)
grid.addWidget(r6, 11,7)
grid.addWidget(s0, 12,1)
grid.addWidget(s1, 12,2)
grid.addWidget(s2, 12,3)
grid.addWidget(s3, 12,4)
grid.addWidget(s4, 12,5)
grid.addWidget(s5, 12,6)
grid.addWidget(s6, 12,7)
grid.addWidget(t0, 13,1)
grid.addWidget(t1, 13,2)
grid.addWidget(t2, 13,3)
grid.addWidget(t3, 13,4)
grid.addWidget(t4, 13,5)
grid.addWidget(t5, 13,6)
grid.addWidget(t6, 13,7)
grid.addWidget(v0, 14,1)
grid.addWidget(v1, 14,2)
grid.addWidget(v2, 14,3)
grid.addWidget(v3, 14,4)
grid.addWidget(v4, 14,5)
grid.addWidget(v5, 14,6)
grid.addWidget(v6, 14,7)
grid.addWidget(w0, 15,1)
grid.addWidget(w1, 15,2)
grid.addWidget(w2, 15,3)
grid.addWidget(w3, 15,4)
grid.addWidget(w4, 15,5)
grid.addWidget(w5, 15,6)
grid.addWidget(w6, 15,7)
grid.addWidget(y0, 16,1)
grid.addWidget(y1, 16,2)
grid.addWidget(y2, 16,3)
grid.addWidget(y3, 16,4)
grid.addWidget(y4, 16,5)
grid.addWidget(y5, 16,6)
grid.addWidget(y6, 16,7)
widget = QWidget()
widget.setLayout(grid)
self.setCentralWidget(widget)
def the_button_was_clicked(self):
print(Card)
Card = random.choice(Cards_List)
window = MainWindow()
window.show()
app.exec()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
