Python: Unterschied zwischen den Versionen

Aus Flinkwiki
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „ssssss“)
 
Zeile 1: Zeile 1:
ssssss
+
 
 +
 
 +
== Sortieren ==
 +
 
 +
Funktion <code>sort()</code>:
 +
''liste.sort()''
 +
 
 +
----
 +
 
 +
=== Bubblesort ===
 +
<nowiki>
 +
def bubblesort(x, Index) :
 +
    for i in range(Index) :
 +
        for j in range(Index-i-1) :
 +
          if x[j] > x[j+1] :
 +
            x[j], x[j+1] = exchange(x[j], x[j+1]);
 +
</nowiki>
 +
Beispiel: <br>''bubblesort(liste,6)<br>print(liste)''
 +
 
 +
----
 +
 
 +
== Tkinter ==
 +
 
 +
Tkinter ist die Python-Schnittstelle zu Tk. Tkint ist ein Akronym für "Tk interface". Tk wurde ursprünglich als eine GUI-Erweiterung für Tcl entwickelt.
 +
* https://www.python-kurs.eu/python_tkinter.php
 +
 
 +
----
 +
=== Import ===
 +
 
 +
Unter Python2 wird ''Tkinter'' großgeschrieben, unter Python3 kleingeschrieben: ''tkinter''.
 +
 
 +
Python2: <code>from Tkinter import *</code>
 +
 
 +
Python3: <code>from tkinter import *</code>
 +
 
 +
----
 +
 
 +
=== Widgets ===
 +
 
 +
    button
 +
    canvas
 +
    checkbutton
 +
    combobox
 +
    entry
 +
    frame
 +
    label
 +
    labelframe
 +
    listbox
 +
    menu
 +
    menubutton
 +
    message
 +
    notebook
 +
    tk_optionMenu
 +
    panedwindow
 +
    progressbar
 +
    radiobutton
 +
    scale
 +
    scrollbar
 +
    separator
 +
    sizegrip
 +
    spinbox
 +
    text
 +
    treeview
 +
 
 +
=== Fenster ===
 +
 
 +
    tk_chooseColor - lässt ein Pop-up-Fenster erscheinen, dass es dem Benutzer ermöglicht eine Farbe aus einer Palette auszuwählen.
 +
    tk_chooseDirectory - Pop-up-Fenster, dass einem Benutzer erlaubt interaktiv ein Verzeichnis auszuwählen.
 +
    tk_dialog - ein Pop-up-Fenster in Form eines Dialogfenster
 +
    tk_getOpenFile - Pop-up-Fenster, dass einem Benutzer erlaubt interaktiv eine Datei zum Öffnen auszuwählen.
 +
    tk_getSaveFile - Pop-up-Fenster, dass einem Benutzer erlaubt interaktiv eine Datei zum Schreiben auszuwählen.
 +
    tk_messageBox - Pop-up-Fenster mit Message.
 +
    tk_popup - Pop-up-Fenster.
 +
    toplevel - erzeugt und verändert Widgets auf höchster Ebene.
 +
 
 +
=== Geometrie-Manager ===
 +
 
 +
    place
 +
    grid
 +
    pack
 +
[https://www.python-kurs.eu/python_tkinter.php]
 +
 
 +
 
 +
----
 +
=== Beispiel Textbox ===
 +
 
 +
<nowiki>
 +
from tkinter import *
 +
 
 +
w1=Tk()
 +
w1.geometry('100x100')
 +
w1.config(bg='#f00')
 +
w1.title('red windows')
 +
 
 +
w2=Tk()
 +
w2.geometry('300x300')
 +
w2.config(bg='grey')
 +
w2.title('grey windows')
 +
 
 +
t1 = Label(w1, text='text in red window', fg='yellow', bg = 'grey')
 +
t1.config(font = ('Times', 25))
 +
t1.pack()
 +
 
 +
w1.mainloop()
 +
w2.mainloop()
 +
</nowiki>
 +
[https://www.youtube.com/watch?v=vtsS2aQ4LLw]
 +
 
 +
----
 +
 
 +
== Turtle ==
 +
 
 +
* https://docs.python.org/2/library/turtle.html
 +
 
 +
 
 +
=== Farben ===
 +
 
 +
turtle.color(rot,grün,blau)
 +
 
 +
* Rot <code>(1,0,0)</code>
 +
* Grün <code>(0,1,0)</code>
 +
* Blau <code>(0,0,1)</code>
 +
* Gelb <code>(1,1,0)</code>
 +
* Cyan <code>(0,1,1)</code>
 +
* Schwarz <code>(0,0,0)</code>
 +
* Weiß <code>(1,1,1)</code>
 +
* Gold <code>(0.9,0.75,0)</code>
 +
* Hellrosa <code>(1,0.7,0.75)</code>
 +
* Orange <code>(1,0.5,0)</code>
 +
* Orange <code>(0.9,0.5,0.15)</code>
 +
 
 +
----
 +
 
 +
== pyperclip ==
 +
 
 +
Installation:
 +
 
 +
<code>apt-get install python-pyperclip</code>
 +
 
 +
Das Modul pyperclip lässt sich aber erst laden nach
 +
 
 +
<code>sudo pip3 install pyperclip</code>
 +
 
 +
----
 +
 
 +
== pygame ==
 +
 
 +
=== Installation ===
 +
Installation in Ubuntu:
 +
pip3 install pygame
 +
<!-- apt-get install python-pygame-->
 +
[https://stackoverflow.com/questions/42961294/no-module-named-pygame]
 +
 
 +
----
 +
 
 +
=== Zeichnen ===
 +
 
 +
Beispiel Regenbogenflagge:
 +
<nowiki>
 +
import pygame
 +
cols = ['#ff0000','#ff8000','#ffff00','#008000','#0000ff','#a000c0']
 +
 +
w, h = 1000, 618                    # width, height
 +
y = h/6                            # width per stripe
 +
 +
d = pygame.display.set_mode((w,h))
 +
for i, c in enumerate(cols):
 +
    d.fill(pygame.Color(c),rect=(0,i*y,w,y*(i+1)))
 +
 +
pygame.display.flip()
 +
pygame.image.save(d,'RainbowFlag.png')
 +
</nowiki>
 +
[http://python3.codes/rainbow-flag-in-pygame/]
 +
 
 +
----
 +
 
 +
== pyinstaller ==
 +
 
 +
Installation:
 +
apt install python-pip
 +
pip install pyinstaller
 +
 
 +
<code>pyinstaller -F</code> erzeugt ein Stand Alone Executable. Dieses liegt dann im Verzeichnis ''dist''.
 +
 
 +
----
 +
 
 +
== Typographie ==
 +
 
 +
Sonderzeichen Herz = <code>u'\u2764'</code>
 +
 
 +
----
 +
== Formatierung ==
 +
 
 +
Formatierung von float-Zahlen ("Gleitkommazahlen") auf 2 Nachkommastellen: <code>'%.2f' % </code>
 +
kapital = 22092.442508224092
 +
print('%.2f' % kapital)
 +
>>> 22092.44
 +
[https://www.gutefrage.net/frage/float-nur-mit-2-nachkommastellen-ausgeben]
 +
 
 +
----
 +
 
 +
== Troubleshooting ==
 +
 
 +
=== Deutsche Sonderzeichen lassen sich in IDLE nicht abspeichern ===
 +
 
 +
Entweder an den Anfang des Skripts folgende Zeile setzen:
 +
# -*- coding: utf-8 -*-
 +
bzw.
 +
# -*- coding: cp1252 -*-
 +
[https://docs.python.org/2.4/lib/standard-encodings.html]
 +
 
 +
oder in IDLE (Shell-Menü) folgende Einstellung vornehmen:
 +
''Options > Configure IDLE > General > Default Source Encoding > UTF-8''
 +
 
 +
----
 +
 
 +
=== Code liegt in Python 2 vor ===
 +
 
 +
>> Umwandlung mit dem Skript ''2to3.py''. Das Skript liegt im Verzeichnis von Python 2.
 +
 
 +
Man kann sich ''2to3.py'' natürlich auch in das lokale Verzeichnis legen. Umwandlung mit
 +
python -w 2to3.py py2Datei.py
 +
 
 +
----
 +
 
 +
== Links und Quellen ==
 +
 
 +
* https://wiki.python.org/moin/
 +
 
 +
* https://python-kurs.eu
 +
 
 +
* http://docs.python.org/2.7/tutorial
 +
 
 +
* http://pythontutor.com/
 +
 
 +
 
 +
----
 +
 
 +
[[Kategorie: Alle]] | [[Kategorie: Programmierung]]

Version vom 26. Oktober 2018, 10:41 Uhr


Sortieren

Funktion sort(): liste.sort()


Bubblesort

def bubblesort(x, Index) :
    for i in range(Index) :
        for j in range(Index-i-1) :
          if x[j] > x[j+1] :
            x[j], x[j+1] = exchange(x[j], x[j+1]);

Beispiel:
bubblesort(liste,6)
print(liste)


Tkinter

Tkinter ist die Python-Schnittstelle zu Tk. Tkint ist ein Akronym für "Tk interface". Tk wurde ursprünglich als eine GUI-Erweiterung für Tcl entwickelt.


Import

Unter Python2 wird Tkinter großgeschrieben, unter Python3 kleingeschrieben: tkinter.

Python2: from Tkinter import *

Python3: from tkinter import *


Widgets

   button
   canvas
   checkbutton
   combobox
   entry
   frame
   label
   labelframe
   listbox
   menu
   menubutton
   message
   notebook
   tk_optionMenu
   panedwindow
   progressbar
   radiobutton
   scale
   scrollbar
   separator
   sizegrip
   spinbox
   text
   treeview

Fenster

   tk_chooseColor - lässt ein Pop-up-Fenster erscheinen, dass es dem Benutzer ermöglicht eine Farbe aus einer Palette auszuwählen.
   tk_chooseDirectory - Pop-up-Fenster, dass einem Benutzer erlaubt interaktiv ein Verzeichnis auszuwählen.
   tk_dialog - ein Pop-up-Fenster in Form eines Dialogfenster
   tk_getOpenFile - Pop-up-Fenster, dass einem Benutzer erlaubt interaktiv eine Datei zum Öffnen auszuwählen.
   tk_getSaveFile - Pop-up-Fenster, dass einem Benutzer erlaubt interaktiv eine Datei zum Schreiben auszuwählen.
   tk_messageBox - Pop-up-Fenster mit Message.
   tk_popup - Pop-up-Fenster.
   toplevel - erzeugt und verändert Widgets auf höchster Ebene.

Geometrie-Manager

   place
   grid
   pack

[1]



Beispiel Textbox

from tkinter import *

w1=Tk()
w1.geometry('100x100')
w1.config(bg='#f00')
w1.title('red windows')

w2=Tk()
w2.geometry('300x300')
w2.config(bg='grey')
w2.title('grey windows')

t1 = Label(w1, text='text in red window', fg='yellow', bg = 'grey')
t1.config(font = ('Times', 25))
t1.pack()

w1.mainloop()
w2.mainloop()

[2]


Turtle


Farben

turtle.color(rot,grün,blau)

  • Rot (1,0,0)
  • Grün (0,1,0)
  • Blau (0,0,1)
  • Gelb (1,1,0)
  • Cyan (0,1,1)
  • Schwarz (0,0,0)
  • Weiß (1,1,1)
  • Gold (0.9,0.75,0)
  • Hellrosa (1,0.7,0.75)
  • Orange (1,0.5,0)
  • Orange (0.9,0.5,0.15)

pyperclip

Installation:

apt-get install python-pyperclip

Das Modul pyperclip lässt sich aber erst laden nach

sudo pip3 install pyperclip


pygame

Installation

Installation in Ubuntu:

pip3 install pygame

[3]


Zeichnen

Beispiel Regenbogenflagge:

import pygame
cols = ['#ff0000','#ff8000','#ffff00','#008000','#0000ff','#a000c0']
 
w, h = 1000, 618                    # width, height
y = h/6                             # width per stripe
 
d = pygame.display.set_mode((w,h))
for i, c in enumerate(cols):
    d.fill(pygame.Color(c),rect=(0,i*y,w,y*(i+1)))
 
pygame.display.flip()
pygame.image.save(d,'RainbowFlag.png')

[4]


pyinstaller

Installation:

apt install python-pip
pip install pyinstaller

pyinstaller -F erzeugt ein Stand Alone Executable. Dieses liegt dann im Verzeichnis dist.


Typographie

Sonderzeichen Herz = u'\u2764'


Formatierung

Formatierung von float-Zahlen ("Gleitkommazahlen") auf 2 Nachkommastellen: '%.2f' %

kapital = 22092.442508224092
print('%.2f' % kapital)
>>> 22092.44

[5]


Troubleshooting

Deutsche Sonderzeichen lassen sich in IDLE nicht abspeichern

Entweder an den Anfang des Skripts folgende Zeile setzen:

# -*- coding: utf-8 -*-

bzw.

# -*- coding: cp1252 -*-

[6]

oder in IDLE (Shell-Menü) folgende Einstellung vornehmen: Options > Configure IDLE > General > Default Source Encoding > UTF-8


Code liegt in Python 2 vor

>> Umwandlung mit dem Skript 2to3.py. Das Skript liegt im Verzeichnis von Python 2.

Man kann sich 2to3.py natürlich auch in das lokale Verzeichnis legen. Umwandlung mit

python -w 2to3.py py2Datei.py 

Links und Quellen



|