SlideShare a Scribd company logo
1 of 22
Download to read offline
Introduktion till
programmering
Filhantering och grafiska gränssnitt
Simon Johansson
mail@simon-johansson.com
Repetition
Felhantering: try/except
Booleska variabler: True & False
Utökad selektion: if/elif/else
Iteration: while & for
Datasamlingar: [], (), {}
Repetition: for-loop & listor
def showContent(list):
for item in list:
print item
def main():
saker = ["Simon", 115, "Mango", False]
showContent(saker)
main()
Simon
115
Mango
False
ex.1
Repetition: while-loop & felhantering
while True:
try:
userInput = int(raw_input("Skriv ett heltal:"))
break
except ValueError:
print "Måste vara ett heltal! Försök igen."
print userInput * 10
ex.2
Repetition: index
lista = ["röd", "grön", "blå"]
3st element
På index 0 finns: röd
0 - röd
1 - grön
2 - blå
ex.3
print str(len(lista)) + "st element"
print "På index 0 finns: " + lista[0]
for i in range(0, len(lista)):
print str(i) + " - " + lista[i]
Persistens
Hur data överlever till nästa programkörning
● Spara i en databas
● Spara i en fil
Vi börjar med att titta på hur vi kan skriva ner data till
en fil från ett Python-program
Spara till fil
highScore = 1000
myFile = open('fil.txt', 'w')
myFile.write(str(highScore))
myFile.close()
ex.4
Sökvägar
#Relativ sökväg
myFile = open('fil.txt', 'w')
#Absolut sökväg
myFile = open('C:/min_mapp/fil.txt', 'w')
Spara till fil
highScore = [250, 375, 400, 480, 520]
myFile = open('fil.txt', 'w')
for item in highScore:
myFile.write(str(item) + 'n')
myFile.close()
ex.5
Spara till fil med felhantering
highScore = [1000, 1500, 2000, 2500, 3000]
try:
myFile = open('fil.txt', 'w')
for item in highScore:
myFile.write(str(item) + 'n')
myFile.close()
except IOError:
print 'Error! Kunde inte skriva till fil'
ex.6
Läsa från fil med felhantering
try:
myFile = open('fil.txt', 'r')
print myFile.read()
myFile.close()
except IOError:
print "Error! Kunde inte läsa från fil"
ex.7
Läsa från fil med felhantering & “with”
try:
with open("fil.txt") as myFile:
print myFile.read()
except IOError:
print "Fel i filläsningen"
ex.8
GUI-bibliotek
PyQt
wxPython
Tkinter - ingår i standardbiblioteket
Python har ett starkt community, många bra plugins att ta
hjälp av.
Tkinter-widgets
Label() - Etikett
Entry() - Inputruta
Text() - Textruta
Checkbutton() - Kryssruta
Button() - Knapp (går att koppla till en funktion)
effbot.org/tkinterbook
GUI-programmering med Tkinter
from Tkinter import *
ex.9
window = Tk()
etikett = Label(window, text="Mitt första GUI!")
etikett.pack()
knapp = Button(window, text="Klicka här!",
bg="blue", fg="white", command=myFunction)
knapp.pack(fill=X)
window.mainloop()
GUI-programmering med Tkinter ex.9
Positionering av widgets
.pack() - Placerar widgets efter varandra
.grid(row=0, column=0) - Placering efter ett rutnät
OBS! Blanda aldrig grid och pack!
effbot.org/zone/tkinter-geometry.htm
Inmatningsruta
textVar = StringVar()
textVar.set("Skriv här!")
inmatning = Entry(window, textvariable=textVar)
inmatning.pack()
#I en annan del av koden
textVar.get()
ex.10
Inmatningsruta ex.10
Tips
● Experimentera med exemplen
● Prova en massa olika Tkinter-widgets
● Ställ en massa frågor på handledningen och i forumet på
iLearn2
Frågor?!

More Related Content

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Introduktion till programmering - filhantering och grafiska gränssnitt