Tkinter messagebox (“Xabarlar qutisi”)

Tkinter messagebox (“Xabarlar qutisi”)
MessageBox
moduli sizning ilovalaringizda xabarlar oynalarini ko'rsatish uchun
ishlatiladi. Ushbu
modul sizga tegishli xabarni ko'rsatish uchun foydalanishingiz mumkin bo'lgan bir
qator
funktsiyalarni taqdim etadi. Ushbu funktsiyalarning ba'zilari showinfo,
showwarning, showerror,
askquestion, askokcancel, askyesno va askretryignore.
Sintaksis :
Ushbu vidjetni yaratish uchun oddiy sintaksis :
tkinter.messagebox.FunctionName (title, message [, options])
Parametrlar :
FunctionName
- Bu tegishli xabar qutisi funktsiyasining nomi.
title
- Bu xabarlar oynasining sarlavhasida ko'rsatiladigan matn.
message
- Bu xabar sifatida ko'rsatiladigan matn.
options
– option lar bu - siz standart xabar qutisini moslashtirish uchun
foydalanishingiz mumkin bo'lgan muqobil tanlovdir. Siz oydalanishingiz mumkin
bo'lgan ba'zi parametrlar standart parametrlar va ota-ona opsiyalaridir. Standart
parametr, xabar oynasida ABORT, RETRY yoki
IGNORE kabi standart tugmani belgilash uchun ishlatiladi. Ota-ona opsiyasi esa
xabar oynasi ko'rsatiladigan oynani belgilash uchun ishlatiladi.
Siz dialog oynasi yordamida quyidagi funktsiyalardan birini ishlatishingiz
mumkin:
showinfo() showwarning() showerror () askquestion()
askokcancel() askyesno () askretrycancel ()
import tkinter as tk
from tkinter import messagebox
def xabar_ko'rsat():
messagebox.showinfo("Xabar", "Salom, bu tkinterda xabarnoma!")
def savol_ber():
javob = messagebox.askyesno("Savol", "Tkinter bilan tanishishga
tayyormisiz?")
if javob:
messagebox.showinfo("Javob", "Ajoyib! Tanishish uchun tayyormisiz.")
else:
messagebox.showinfo("Javob", "Xayr, boshqa borada tanishamiz.")
# Asosiy oynani yaratamiz
oyna = tk.Tk()
oyna.title("Tkinter Xabarnoma Misoli")
# Xabar ko'rsatish tugmasi
xabar_tugmasi = tk.Button(oyna, text="Xabar ko'rsatish",
command=xabar_ko'rsat)
# Savol bermoq tugmasi
savol_tugmasi = tk.Button(oyna, text="Savol bermoq",
command=savol_ber)
# Obyekt-larni oynaga qo'shamiz
xabar_tugmasi.pack(pady=10)
savol_tugmasi.pack(pady=10)
# Tkinter hodisalarini boshlaymiz
oyna.mainloop()
Combobox haqida qisqacha:
tkinter da Combobox elementini (Ro'yxat tanlov oynasi) yaratish uchun,
ttk
(themed tkinter) modulini ishlatishingiz mumkin. Quyidagi kodni ko'rib
chiqaylik:
import tkinter as tk
from tkinter import ttk
def tanlov_oyincha(*args):
tanlov_tekshir.set("Siz " + tanlov_var.get() + "ni tanladingiz.")
# Asosiy oynani yaratamiz
oyna = tk.Tk()
oyna.title("Tkinter Combobox Misoli")
# Tanlov uchun ro'yxatni tuzamiz
tanlov_var = tk.StringVar()
tanlov = ttk.Combobox(oyna, textvariable=tanlov_var)
tanlov["values"] = ("Variant 1", "Variant 2", "Variant 3", "Variant 4")
tanlov.grid(column=0, row=0, padx=10, pady=10)
tanlov.current(0) # Boshlang'ich tanlovni tanlash
# Tanlovni tekshiruvchi Label
tanlov_tekshir = tk.StringVar()
tanlov_tekshir_label = ttk.Label(oyna, textvariable=tanlov_tekshir)
tanlov_tekshir_label.grid(column=1, row=0, padx=10, pady=10)
# Tanlov o'zgarganda tanlov_oyincha funksiyasini chaqirish
tanlov_var.trace("w", tanlov_oyincha)
# Tkinter hodisalarini boshlaymiz
oyna.mainloop()
Dasturimizga
run
berganimizdan sahifada ko’rishingiz mumkin bo’lgan
oyna quyidagidan iborat
Quyidagi oynada ttk kutubxonasidan treeview elementi chaqirib olingan
Undagi heading lar (id , ism ,tashkilot , manzil , telefon )
Agarda biz “Add ” tugmasini bosganimizda quyidagi oyna hosil bo’ladi
Tashkilotga xodim qo’shish oynasida foydalanuvchi telefon raqamlari ,
ismini va boshqa malumotlarini kiritishi mumkin .
Tashkilot nomi degan joyiga etibor bersak “+” tugmachasi bor bu
tugmani bosib tashkilot nomini kiritishimiz mumkin . “+” tugmasi
bosilganda quyidagi oyna hosil bo’ladi
Kod Tuzilishi:
Kodni o'qib yozish va saqlashga oson bo'lishi uchun uni kichik
funksiyalarga yoki klasslarga ajratish yo’lidan foydalandim . Bu, kodni
tushunish va yaxshi o'zgartirish uchun yordam bera olishi mumkin.
Foydalanuvchi Interfeysi Yaxshilanishi:
Umumiy foydalanuvchi interfeysni yaxshilash uchun, foydalanuvchiga
qanday qilib ishlashini yaxshi tushuntirish uchun yorliqlar yoki boshqa
elementlarni qo'shish mumkin.
Ma'lumotlar Bazasini Yopish:
Dastur yakunlanishida ma'lumotlar bazasini yopish juda muhim. Bu
uchun oynaga yopish hodisalarga bog'langan metod yozish yaxshi
bo'ladi.
Mavzu doirasida ishlatilgan fayldagi kodlar:
import tkinter as tk
from tkinter import ttk
import sqlite3
class XodimlarOyna:
def __init__(self, oyna):
self.oyna = oyna
self.oyna.title("Xodimlar")
# Ma'lumotlar bazasini yaratish
self.yaratish_baza()
# Xodimlar jadvalini chiqarish
self.xodimlar_jadvalini_chiqar()
# Xodim qo'shish uchun Add tugmasi
self.add_tugma = tk.Button(oyna, text="Add",
command=self.xodim_qoshish)
self.add_tugma.pack(pady=10)
def yaratish_baza(self):
conn = sqlite3.connect("xodimlar_baza.db")
cursor = conn.cursor()
# Xodimlar jadvalini yaratish
cursor.execute('''CREATE TABLE IF NOT EXISTS xodimlar
(id INTEGER PRIMARY KEY AUTOINCREMENT,
ism TEXT,
tashkilot TEXT,
manzil TEXT,
telefon TEXT)''')
# Tashkilotlar jadvalini yaratish
cursor.execute('''CREATE TABLE IF NOT EXISTS tashkilotlar
(id INTEGER PRIMARY KEY AUTOINCREMENT,
nomi TEXT,
malumot TEXT)''')
conn.commit()
conn.close()
def xodimlar_jadvalini_chiqar(self):
conn = sqlite3.connect("xodimlar_baza.db")
cursor = conn.cursor()
# Xodimlar jadvalidan malumotlarni olish
cursor.execute("SELECT * FROM xodimlar")
xodimlar_malumotlari = cursor.fetchall()
conn.close()
# Xodimlar jadvalini chiqarish
jadval = ttk.Treeview(self.oyna, columns=("ID", "Ism", "Tashkilot",
"Manzil", "Telefon"))
jadval.heading("#0", text="ID")
jadval.column("#0", width=50, anchor="center")
jadval.heading("#1", text="Ism")
jadval.heading("#2", text="Tashkilot")
jadval.heading("#3", text="Manzil")
jadval.heading("#4", text="Telefon")
for xodim in xodimlar_malumotlari:
jadval.insert("", tk.END, values=xodim)
jadval.pack(pady=10)
def xodim_qoshish(self):
# Create a new Toplevel window for data input
input_window = tk.Toplevel(self.oyna)
input_window.title("Xodim qo'shish")
# Combobox for user input
tashkilot_values = self.get_tashkilot_values()
self.tashkilot_combobox = ttk.Combobox(input_window,
values=tashkilot_values, width=30)
self.tashkilot_combobox.grid(row=1, column=1, padx=10, pady=5)
# Labels for entry widgets
tk.Label(input_window, text="Xodim ismi:").grid(row=0, column=0,
padx=10, pady=5, sticky="w")
tk.Label(input_window, text="Tashkilot nomi:").grid(row=1, column=0,
padx=10, pady=5, sticky="w")
tk.Label(input_window, text="Manzil:").grid(row=2, column=0, padx=10,
pady=5, sticky="w")
tk.Label(input_window, text="Telefon raqami:").grid(row=3, column=0,
padx=10, pady=5, sticky="w")
# Entry widgets for user input
self.ism_entry = tk.Entry(input_window, width=30)
self.ism_entry.grid(row=0, column=1, padx=10, pady=5)
self.manzil_entry = tk.Entry(input_window, width=30)
self.manzil_entry.grid(row=2, column=1, padx=10, pady=5)
self.telefon_entry = tk.Entry(input_window, width=30)
self.telefon_entry.grid(row=3, column=1, padx=10, pady=5)
# Function to handle the button click and insert data into the database
def insert_data():
xodim_ismi = self.ism_entry.get()
tashkilot_nomi = self.tashkilot_combobox.get()
manzil = self.manzil_entry.get()
telefon_raqam = self.telefon_entry.get()
# Xodimni ma'lumotlar bazasiga qo'shish
conn = sqlite3.connect("xodimlar_baza.db")
cursor = conn.cursor()
# Tashkilotni olish yoki qo'shish
tashkilot_id = self.get_or_add_tashkilot(cursor, tashkilot_nomi)
cursor.execute("INSERT INTO xodimlar (ism, tashkilot, manzil, telefon)
VALUES (?, ?, ?, ?)",
(xodim_ismi, tashkilot_id, manzil, telefon_raqam))
conn.commit()
conn.close()
# Close the input window
input_window.destroy()
# Xodimlar jadvalini yangilash
self.xodimlar_jadvalini_chiqar()
# Button to submit data
submit_button = tk.Button(input_window, text="Add",
command=insert_data)
submit_button.grid(row=4, column=0, columnspan=2, pady=10)
# Button to add a new company
add_company_button = tk.Button(input_window, text="+",
command=self.add_new_company)
add_company_button.grid(row=1, column=2, padx=5)
def get_tashkilot_values(self):
conn = sqlite3.connect("xodimlar_baza.db")
cursor = conn.cursor()
cursor.execute("SELECT nomi FROM tashkilotlar")
tashkilotlar = cursor.fetchall()
conn.close()
return [t[0] for t in tashkilotlar]
def get_or_add_tashkilot(self, cursor, tashkilot_nomi):
# Check if the company already exists
cursor.execute("SELECT id FROM tashkilotlar WHERE nomi=?",
(tashkilot_nomi,))
existing_company = cursor.fetchone()
if existing_company:
return existing_company[0]
else:
# If the company does not exist, add it to the database
cursor.execute("INSERT INTO tashkilotlar (nomi, malumot) VALUES (?,
?)", (tashkilot_nomi, ""))
return cursor.lastrowid
def add_new_company(self):
# Create a new Toplevel window for adding a new company
company_window = tk.Toplevel(self.oyna)
company_window.title("Add New Company")
tk.Label(company_window, text="Tashkilot nomi:").grid(row=0, column=0,
padx=10, pady=5, sticky="w")
new_company_entry = tk.Entry(company_window, width=30)
new_company_entry.grid(row=0, column=1, padx=10, pady=5)
# Function to handle the button click and insert a new company
def insert_company():
new_company_name = new_company_entry.get()
# Add the new company to the database
conn = sqlite3.connect("xodimlar_baza.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO tashkilotlar (nomi, malumot) VALUES (?,
?)", (new_company_name, ""))
conn.commit()
conn.close()
# Update the values in the main input window
self.tashkilot_combobox["values"] = self.get_tashkilot_values()
self.tashkilot_combobox.set(new_company_name)
# Close the company window
company_window.destroy()
# Button to submit the new company
submit_company_button = tk.Button(company_window, text="Add",
command=insert_company)
submit_company_button.grid(row=1, column=0, columnspan=2, pady=10)
if __name__ == "__main__":
oyna = tk.Tk()
xodimlar_oyna = XodimlarOyna(oyna)
oyna.mainloop()
|