TI/PyQt5

Z Brain-wiki
Wersja z dnia 10:50, 29 maj 2019 autorstwa Tgub (dyskusja | edycje)
(różn.) ← poprzednia wersja | przejdź do aktualnej wersji (różn.) | następna wersja → (różn.)

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_())

kółko i krzyzyk z losowym ruchem komputera

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import numpy as np
import random

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]))
    
def gdzieZera(tablica):
    return np.transpose(np.nonzero(tablica<1)).tolist()

def losowyRuchKomputera(numerGracza,tablica):
    print('robie losowy ruch')
    mozliweRuchy=gdzieZera(tablica)
    if mozliweRuchy:
        x,y=random.choice(mozliweRuchy)
        tablica[x,y]=numerGracza


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_())
            losowyRuchKomputera(2,a)
            rysuj()
            if czyKoniec(a):
                print(czyKoniec(a))
                sys.exit(app.exec_())
            

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_())

Przykładowe rozwiazanie zadania PlusMinus

import sys
from PyQt5.QtCore import Qt 
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
import PyQt5.QtGui as QtGui
 
class App(QWidget):
 
    def __init__(self):
        super().__init__()
        self.title = 'Guzik'
        self.left = 10
        self.top = 10
        self.width = 420
        self.height = 420
        self.liczba=0
        self.initUI()
 
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        
        self.napis = QLabel(str(self.liczba), self)
        self.napis.move(10,10)
        self.napis.resize(400,200)
        self.napis.setAlignment(Qt.AlignCenter)
        self.napis.setFont(QtGui.QFont("Arial", 40, QtGui.QFont.Bold))
        
        
        self.guzikPlus=QPushButton('+', self)
        self.guzikPlus.move(10,210)
        self.guzikPlus.resize(200,200)
        
        self.guzikMinus=QPushButton('-', self)
        self.guzikMinus.move(210,210)
        self.guzikMinus.resize(200,200)
        
        
        self.guzikPlus.clicked.connect(self.clickPlus)
        self.guzikMinus.clicked.connect(self.clickMinus)
        self.show()
 
    def clickPlus(self):
        self.liczba+=1
        self.napis.setText(str(self.liczba))
        
    def clickMinus(self):
        self.liczba-=1
        self.napis.setText(str(self.liczba))
        
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())