TI/PyQt5: Różnice pomiędzy wersjami
Z Brain-wiki
Linia 72: | Linia 72: | ||
for y in range(3): | for y in range(3): | ||
MojGuzik(x,y, self) | MojGuzik(x,y, self) | ||
+ | self.show() | ||
+ | |||
+ | if __name__ == '__main__': | ||
+ | app = QApplication(sys.argv) | ||
+ | ex = App() | ||
+ | sys.exit(app.exec_()) | ||
+ | </source> | ||
+ | |||
+ | kółko i krzyzyk dla dwóch graczy | ||
+ | <source lang="python"> | ||
+ | import sys | ||
+ | from PyQt5.QtWidgets import QApplication, QWidget, QPushButton | ||
+ | import numpy as np | ||
+ | |||
+ | numerGracza=1 | ||
+ | N=5 | ||
+ | a=np.zeros((N,N)) | ||
+ | |||
+ | def znak(liczba): | ||
+ | if liczba==1: return 'O' | ||
+ | if liczba==2: return 'X' | ||
+ | return ' ' | ||
+ | |||
+ | def czyKoniec(tablica): | ||
+ | n=tablica.shape[0] | ||
+ | if tablica.shape[1]!=n: return None | ||
+ | |||
+ | for numerGracza in [1,2]: #dla kazdego gracza | ||
+ | a=(tablica==numerGracza) | ||
+ | for i in range(n): | ||
+ | if ((a[i,:]).sum()==n) or ((a[:,i]).sum()==n): | ||
+ | return 'wygral gracz numer '+str(numerGracza) #sprawdzamy wiersze i kolumny | ||
+ | if (sum([a[i,i] for i in range(n)])==n) or (sum([a[i,n-1-i] for i in range(n)])==n): | ||
+ | return 'wygral gracz numer '+str(numerGracza) #sprawdzamy przekatne | ||
+ | if ((tablica>0).sum()==n*n): return 'remis' | ||
+ | return False | ||
+ | |||
+ | def rysuj(): | ||
+ | for x in range(N): | ||
+ | for y in range(N): | ||
+ | ex.guziki[x][y].setText(znak(a[x,y])) | ||
+ | |||
+ | |||
+ | class MojGuzik(QPushButton): | ||
+ | def __init__(self,i,j,*args): | ||
+ | super().__init__('',*args) | ||
+ | self.i=i | ||
+ | self.j=j | ||
+ | self.move(10+100*self.i,10+100*self.j) | ||
+ | self.resize(100,100) | ||
+ | self.clicked.connect(self.po_kliknieciu) | ||
+ | def po_kliknieciu(self): | ||
+ | global numerGracza | ||
+ | if a[self.i,self.j]==0: | ||
+ | a[self.i,self.j]=numerGracza | ||
+ | rysuj() | ||
+ | if czyKoniec(a): | ||
+ | print(czyKoniec(a)) | ||
+ | sys.exit(app.exec_()) | ||
+ | if numerGracza==1: | ||
+ | numerGracza=2 | ||
+ | else: | ||
+ | numerGracza=1 | ||
+ | |||
+ | |||
+ | class App(QWidget): | ||
+ | def __init__(self): | ||
+ | super().__init__() | ||
+ | self.title = 'Guzik' | ||
+ | self.left = 10 | ||
+ | self.top = 10 | ||
+ | self.width=20+100*N | ||
+ | self.height=20+100*N | ||
+ | self.guziki=[] | ||
+ | self.initUI() | ||
+ | |||
+ | def initUI(self): | ||
+ | self.setWindowTitle(self.title) | ||
+ | self.setGeometry(self.left, self.top, self.width, self.height) | ||
+ | for x in range(N): | ||
+ | temp=[] | ||
+ | for y in range(N): | ||
+ | temp.append(MojGuzik(x,y, self)) | ||
+ | self.guziki.append(temp) | ||
self.show() | self.show() | ||
Wersja z 08:59, 29 maj 2019
przyklad 1
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Guzik'
self.left = 10
self.top = 10
self.width = 220
self.height = 220
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.guzik= QPushButton('X', self)
self.guzik.move(10,10)
self.guzik.resize(200,200)
self.guzik.clicked.connect(self.on_click)
self.show()
def on_click(self):
if self.guzik.text()=='X':
self.guzik.setText('O')
else:
self.guzik.setText('X')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
GUI do kółko i krzyzyk
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
class MojGuzik(QPushButton):
def __init__(self,i,j,*args):
super().__init__('',*args)
self.i=i
self.j=j
self.move(10+100*self.i,10+100*self.j)
self.resize(100,100)
self.clicked.connect(self.po_kliknieciu)
def po_kliknieciu(self):
if self.text()=='X':
self.setText('O')
else:
self.setText('X')
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Guzik'
self.left = 10
self.top = 10
self.width = 320
self.height = 320
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
for x in range(3):
for y in range(3):
MojGuzik(x,y, self)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
kółko i krzyzyk dla dwóch graczy
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import numpy as np
numerGracza=1
N=5
a=np.zeros((N,N))
def znak(liczba):
if liczba==1: return 'O'
if liczba==2: return 'X'
return ' '
def czyKoniec(tablica):
n=tablica.shape[0]
if tablica.shape[1]!=n: return None
for numerGracza in [1,2]: #dla kazdego gracza
a=(tablica==numerGracza)
for i in range(n):
if ((a[i,:]).sum()==n) or ((a[:,i]).sum()==n):
return 'wygral gracz numer '+str(numerGracza) #sprawdzamy wiersze i kolumny
if (sum([a[i,i] for i in range(n)])==n) or (sum([a[i,n-1-i] for i in range(n)])==n):
return 'wygral gracz numer '+str(numerGracza) #sprawdzamy przekatne
if ((tablica>0).sum()==n*n): return 'remis'
return False
def rysuj():
for x in range(N):
for y in range(N):
ex.guziki[x][y].setText(znak(a[x,y]))
class MojGuzik(QPushButton):
def __init__(self,i,j,*args):
super().__init__('',*args)
self.i=i
self.j=j
self.move(10+100*self.i,10+100*self.j)
self.resize(100,100)
self.clicked.connect(self.po_kliknieciu)
def po_kliknieciu(self):
global numerGracza
if a[self.i,self.j]==0:
a[self.i,self.j]=numerGracza
rysuj()
if czyKoniec(a):
print(czyKoniec(a))
sys.exit(app.exec_())
if numerGracza==1:
numerGracza=2
else:
numerGracza=1
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Guzik'
self.left = 10
self.top = 10
self.width=20+100*N
self.height=20+100*N
self.guziki=[]
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
for x in range(N):
temp=[]
for y in range(N):
temp.append(MojGuzik(x,y, self))
self.guziki.append(temp)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())