A cikin duniyar shirye-shirye, mu'amalar masu amfani da hoto suna taka muhimmiyar rawa wajen samar da ƙwarewar hulɗa da mai amfani. Ɗaya daga cikin shahararrun ɗakin karatu a Python wanda ke ba mu damar ƙirƙirar aikace-aikacen GUI cikin sauƙi shine ** Tkinter **. A yau, za mu bincika yadda ake ƙirƙirar maɓallin Tkinter, kuma da danna shi, za a nuna alamar. Za mu rarraba sassa daban-daban na lambar kuma mu koyi game da amfani da mahimmancin takamaiman ayyuka da ɗakunan karatu.
Tkinter: Bayani
Tkinter yana nufin "Tk Interface," kuma ita ce daidaitaccen ƙirar Python zuwa kayan aikin Tk GUI. Akwai shi akan yawancin dandamali na Unix, da macOS da Windows. Tkinter yana ba da iko iri-iri, kamar maɓalli, lakabi, da akwatunan rubutu da aka yi amfani da su a cikin aikace-aikacen GUI. Ana kiran waɗannan abubuwan sarrafawa Widgets ta inda kowane widget din ke taka takamaiman rawa wajen samar da ayyuka da mu'amala ga aikace-aikacen.
Ƙirƙirar Maɓalli tare da Latsa taron don Nuna Lakabi
Don cim ma aikin ƙirƙirar maɓallin Tkinter wanda ke nuna alamar lokacin da aka danna, muna buƙatar bin waɗannan matakan:
1. Shigo da ɗakunan karatu da ake buƙata.
2. Ƙirƙiri babban abin taga.
3. Ƙirƙiri widget din maɓalli kuma saita taron latsawa.
4. Ƙirƙiri widget ɗin lakabi kuma ayyana rubutun da za a nuna.
5. Ƙara lakabin da maɓallin widgets a cikin taga.
6. Fara babban taron madauki.
import tkinter as tk def on_button_click(): label.config(text="Button clicked!") root = tk.Tk() button = tk.Button(root, text="Click me!", command=on_button_click) button.pack() label = tk.Label(root, text="Label will update when button clicked") label.pack() root.mainloop()
Bari mu nutse cikin bayanin mataki-mataki na lambar da ke sama.
Fahimtar Code
Da farko, muna shigo da Tkinter dakin karatu kamar haka:
import tkinter as tk
Sa'an nan kuma mu ƙirƙiri wani aiki mai suna a kan_button_danna() wanda zai ɗauki alhakin canza rubutun alamar lokacin da aka danna maɓallin. Za mu saita alamar ta amfani da hanyar "config()" don sabunta sifa ta "rubutu":
def on_button_click(): label.config(text="Button clicked!")
Na gaba, muna ƙirƙirar babban matakin taga abu "tushen" ta amfani da umarni mai zuwa:
root = tk.Tk()
Bayan haka, muna ƙirƙirar maɓallin widget kuma saita sifa ta rubutun zuwa "Danna ni!". Mun kuma wuce da a kan_button_danna() aiki zuwa sifa ta Umurni, don aiwatar da shi akan maɓallin danna:
button = tk.Button(root, text="Click me!", command=on_button_click) button.pack()
Yanzu mun ƙirƙira da alamar widget tare da saita farkon rubutunsa azaman "Label zai ɗaukaka lokacin da aka danna maballin", sannan ƙara shi zuwa taga ta amfani da fakitin ():
label = tk.Label(root, text="Label will update when button clicked") label.pack()
A ƙarshe, mun fara da babban taron madauki tare da layi mai zuwa:
root.mainloop()
Bayan aiwatar da lambar, taga mai maɓalli da lakabi zai bayyana. Da zarar an danna maɓallin, rubutun alamar zai canza bisa ga ma'anar aikin.
Tare da wannan aiwatarwa, mun sami nasarar ƙirƙirar aikace-aikacen mai sauƙi amma mai mu'amala ta amfani da abubuwan Tkinter daban-daban, kamar maɓalli, lakabi, da abubuwan da suka faru, suna ba masu amfani damar hango duniyar mai ƙarfi na mu'amalar mai amfani da hoto.