Cov txheej txheem:

Pib Nrog Raspberry Pi GUI: 8 Kauj Ruam
Pib Nrog Raspberry Pi GUI: 8 Kauj Ruam

Video: Pib Nrog Raspberry Pi GUI: 8 Kauj Ruam

Video: Pib Nrog Raspberry Pi GUI: 8 Kauj Ruam
Video: Ib xyoo laus zus Tub huas Xyooj 2023 2024, Kaum ib hlis
Anonim
Pib Nrog Raspberry Pi GUI
Pib Nrog Raspberry Pi GUI

Yog li koj muaj Raspberry Pi thiab lub tswv yim txias, tab sis koj yuav ua li cas thiaj yooj yim li lub xov tooj smartphone rau koj cov neeg siv cuam tshuam nrog?

Tsim kom muaj Cov Neeg Siv Khoom Siv Duab (GUI) yog qhov yooj yim heev, thiab nrog qee qhov ua siab ntev koj tuaj yeem tsim cov phiaj xwm zoo.

Kauj Ruam 1: Cov Txheej Txheem Dav Dav

Ib qho ntawm cov cuab yeej muaj zog tshaj plaws uas Raspberry Pi muab ntau dua lwm cov micros, yog qhov nrawm thiab yooj yim koj tuaj yeem tsim Cov Duab Siv Sib Txuas (GUI) rau koj txoj haujlwm.

Ib txoj hauv kev kom ua tiav qhov no, tshwj xeeb yog tias koj muaj lub vijtsam puv (lossis lub vijtsam txheem thiab cov cuab yeej siv xws li nas), nws tau dhau los ua qhov txaus ntshai!

Rau lub hom phiaj ntawm kab lus no, peb yuav siv Python 3 nrog Tkinter:

Lub tsev qiv ntawv muaj zog rau kev txhim kho daim duab siv interface (GUI) daim ntawv thov, ntawm Raspberry Pi qhov chaw tsim khoom muaj kev txhawj xeeb.

Tkinter yog tej zaum feem ntau siv nrog Python, thiab muaj peev txheej ntau nyob hauv is taws nem.

Kauj Ruam 2: "Nyob Zoo Ntiaj Teb" hauv Tkinter

Duab
Duab

Peb tab tom siv aRaspberry Pi thauj khoom nrog Raspbian Stretch OS.

Txhawm rau khiav peb Tkinter GUI Cov Ntawv Thov. peb kuj tseem tuaj yeem siv lwm yam kev ua haujlwm uas tau nruab sej.

Raspbian los nrog ob qho Python 2, Python 3 thiab Tkinter lub tsev qiv ntawv teeb tsa.

Txhawm rau txheeb xyuas qhov twg koj tau teeb tsa, los ntawm lub davhlau ya nyob twg khiav:

python3 -hloov tshiab

Tsim cov ntawv tshiab hu ua app.py thiab nkag mus rau qhov chaws hauv qab qhia hauv qab no:

#!/usr/bin/nab hab sej

los ntawm tkinter ntshuam * # ntshuam Tkinter lib hauv paus = Tk () # tsim lub hauv paus lub hauv paus root.wm_title ("Nyob Zoo Ntiaj Teb") # teeb tsa lub npe ntawm lub qhov rais root.mainloop () # pib GUI voj

Yog tias koj tsis siv IDE, khiav cov lus txib hauv qab no hauv lub davhlau ya nyob twg los ntawm cov npe uas muaj koj li Python code los ua haujlwm.

sej 3 app.py

Kauj Ruam 3: Kho lub Qhov Rai

Customizing Qhov rai
Customizing Qhov rai

Tam sim no saib seb yuav kho lub qhov rai no li cas.

Xim tom qab

root.configure (bg = "dub") # hloov xim keeb kwm yav dhau los rau "dub"

los yog

root.configure (bg = " # F9273E") # siv tus lej xim hex

Qhov rai qhov ntev

root.geometry ("800x480") # qhia lub qhov rai qhov ntev

los yog

root.attributes ("-puv npo", Tseeb) # teeb tsa rau puv npo

Nco ntsoov tias koj yuav raug daig hauv hom puv npo yog tias koj tsis tsim txoj hauv kev kom tawm mus

# peb tuaj yeem tawm mus thaum peb nias tus yuam sij khiav

def end_fullscreen (xwm txheej): root.attributes ("-puv npo", Tsis tseeb) root.bind ("", end_fullscreen)

Kauj ruam 4: Widgets hauv Tkinter

Widgets hauv Tkinter
Widgets hauv Tkinter
Widgets hauv Tkinter
Widgets hauv Tkinter

Tkinter suav nrog ntau yam sib txawv los pab koj tsim cov neeg siv uas tsim nyog tshaj plaws. Widgets koj tuaj yeem siv suav nrog: • lub npov ntawv

• khawm

• khawm khawm

• swb

• lub npov npe

• khawm xov tooj cua

• lwm yam..

Tam sim no peb tuaj yeem ntxiv qee cov cuab yeej xws li ntawv, khawm thiab cov tswv yim.

Ntxiv Widgets

Cov ntawv

label_1 = Label (hauv paus, ntawv = "Nyob Zoo, Ntiaj Teb!")

Ua ntej nws pom hauv lub qhov rais tab sis, peb yuav tsum teeb tsa nws txoj haujlwm. Peb yuav siv daim phiaj qhia chaw.

label_1.grid (kab = 0, kab ntawv = 0) # teeb tsa txoj haujlwm

Nkag Mus Nkag

label_1 = Label (hauv paus, ntawv = "Nyob Zoo, Ntiaj Teb!", font = "Verdana 26 bold, fg ="#000 ", bg ="#99B898 ")

label_2 = Daim ntawv lo (hauv paus, ntawv = "Koj lub npe yog dab tsi?", qhov siab = 3, fg = "#000", bg = "#99B898") entry_1 = Nkag mus (hauv paus)#nkag nkag nkag label_1.grid (kab = 0, kab ntawv = 0) label_2.grid (kab = 1, kab ntawv = 0) nkag_1.grid (kab = 1, kab ntawv = 1)

Nyees khawm

#Ntxiv ib lub pob sab hauv lub qhov rais

Khawm = Khawm (hauv paus, ntawv = "Xa") Button.grid (row = 2, column = 1)

Kauj ruam 5: Ntxiv Logic

Ntxiv Logic
Ntxiv Logic
Ntxiv Logic
Ntxiv Logic

Tam sim no peb muaj daim ntawv yooj yim, txawm li cas los nyem rau ntawm lub pob tsis ua dab tsi !!

Peb yuav tshawb nrhiav yuav teeb tsa qhov xwm txheej li cas ntawm cov nyees khawm thiab khi nws rau txoj haujlwm uas ua tiav thaum nyem.

Rau lub hom phiaj no peb yuav hloov daim ntawv lo_1 kom pom "Nyob zoo + cov ntawv nkag mus rau hauv kev nkag". Thaum koj xaiv khawm xa.

Rub tawm cov cai hauv qab no thiab khiav nws.

Kauj Ruam 6: LED Tswj

LED Tswj
LED Tswj
LED Tswj
LED Tswj
LED Tswj
LED Tswj
LED Tswj
LED Tswj

Txog tam sim no peb pom yuav ua li cas ntxiv khawm rau ntawm lub qhov rais thiab ntxiv cov laj thawj rau nws txhawm rau ua qhov kev nqis tes ua.

Tam sim no, peb yuav hloov txoj cai me ntsis. Yog li peb yuav tsim ib daim ntawv thiab ntxiv ob lub khawm rau nws. Ib qho los qhib lub LED qhib/tawm, thiab lwm qhov kom tawm ntawm txoj haujlwm.

Nco tseg: Nco ntsoov tias koj tau hloov kho koj li Raspberry ua ntej pib, thiab tias koj muaj GPIO lub tsev qiv ntawv teeb tsa, Qhib lub qhov rai hais kom ua thiab nkag mus rau hauv qab no GPIO tsev qiv ntawv nruab. Qhib lub qhov rai hais kom ua thiab sau cov hauv qab no:

$ sudo apt-tau hloov tshiab

$ sudo apt-tau nruab python-rpi.gpio python3-rpi.gpio

Tsim:

Qhov Yuav Tsum Tau:

1 x Raspberry Pi 3

1 x LED

1 x 330Ω Resistor

Tsev Tsim Vaj Tsev:

Ua raws li cov duab saum toj no.

Ua tib zoo saib xyuas LED thiab tus pin uas txuas nrog (GPIO23).

Kauj Ruam 7: Ntxiv Servo Motor Controller

Ntxiv Servo Motor Controller
Ntxiv Servo Motor Controller
Ntxiv Servo Motor Controller
Ntxiv Servo Motor Controller
Ntxiv Servo Motor Controller
Ntxiv Servo Motor Controller

Peb yuav txav mus rau lwm yam uas tsis yog khawm peb kuj tseem tuaj yeem siv ntau yam tswv yim los tswj PWM (Pulse Width Modulation) cov txiaj ntsig los ntawm Raspberry Pi.

Lub cav servo yog qhov kev xaiv zoo nws txhais PWM teeb liab mus rau lub kaum ntse ntse.

Tsim:

Qhov Yuav Tsum Tau:

1 x Raspberry Pi 3

1 x LED

1 x 330Ω Resistor

1 x Servo Tsav

Tsev Tsim Vaj Tsev:

Ua raws daim duab qhia saum toj no (LED txuas nrog GPIO 23, Servo Tsav txuas nrog GPIO 18).

Txheeb cov vis dis aus yog tias koj daig.

Kauj Ruam 8: Xaus

Muaj koj muaj nws! Tawm mus thiab kov yeej qee lub tswv yim UI zoo kawg!

Yog tias koj muaj lus nug ntawm chav kawm koj tuaj yeem tawm lus.

Yog xav paub ntau ntxiv txog kuv cov haujlwm thov mus saib kuv tus channel

kuv tus YouTube

kuv tus Twitter

myLinkedin

Ua tsaug rau kev nyeem cov lus qhia no ^^ thiab muaj hnub zoo. Saib koj. Ahmed Nouira: koj puas xav tau ntau tus thwjtim?

Pom zoo: