0

I had two codes, since I combined them, chechbox always = 0 (When I run the code separately, it works fine)

from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from convert_number2text import convert_ordinary
from pyperclip import copy, paste
import csv
from jdatetime import datetime, date
from show_factor import show_factors
from os import listdir

class Program:

    def __init__(self):
        self.root = Tk()
        self.root.title("Accounting")
        self.root.minsize(800, 600)
        Button(self.root, text="فاکتور نوشتن", font=("",20),bg="red", command=self.factor_new).pack()
        Button(self.root, text="فاکتور های ثبت شده", font=("",20),bg="red", command=show_factors).pack()
        Button(self.root, text="افزودن کارت", font=("",20),bg="red").pack()


        self.root.mainloop()





    def show_factors():
        win_show_factor = Tk()
        win_show_factor.title("show factors")
        win_show_factor.minsize(940, 600)

        Label(win_show_factor, text="کد فاکتور", font=("",16)).place(x=760 ,y=10)
        entry_code_factor = Entry(win_show_factor, font=("", 16), width=7)
        entry_code_factor.place(x=755, y=40)
        Label(win_show_factor, text="نام مشتری", font=("",16)).place(x=760, y=80)
        entry_name_factor = Entry(win_show_factor, font=("", 16))
        entry_name_factor.place(x=670, y=110)
        Label(win_show_factor, text="اطلاعات فاکتور", font=("",16)).place(x=240, y=10)
        entry_info_factor = Text(win_show_factor, font=("", 13), width=30, height=7)
        entry_info_factor.place(x=160, y=40)
        
        Label(win_show_factor, text="سال", font=("", 16)).place(x=540,y=5)
        combo_y= ttk.Combobox(win_show_factor, values=["1400"])
        for i in range(1401, 1420):
            combo_y['values'] += (i,)
        combo_y.set("1400")
        combo_y.place(x=480,y=35)
        Label(win_show_factor, text="ماه", font=("", 16)).place(x=540, y=60)
        combo_m= ttk.Combobox(win_show_factor, values=["1"])
        for i in range(2, 13):
            combo_m['values'] += (i,)
        combo_m.set("1")
        combo_m.place(x=480, y=90)  
        Label(win_show_factor, text="روز", font=("", 16)).place(x=530, y=115)
        combo_d= ttk.Combobox(win_show_factor, values=["1"])
        for i in range(2, 32):
            combo_d['values'] += (i,)
        combo_d.set("1")
        combo_d.place(x=480, y=150)
        
        list_box_find = ttk.Treeview(win_show_factor, column=("c1", "c2", "c3", "c4", "c5"), show='headings')
        list_box_find.column("# 1", anchor=CENTER,width=220)
        list_box_find.heading("# 1", text="نام مشتری")
        list_box_find.column("# 2", anchor=CENTER, width=60)
        list_box_find.heading("# 2", text="کد فاکتور")
        list_box_find.column("# 3", anchor=CENTER, width=150)
        list_box_find.heading("# 3", text="تاریخ")
        list_box_find.column("# 4", anchor=CENTER, width=100)
        list_box_find.heading("# 4", text="مبلغ فاکتور")
        list_box_find.column("# 5", anchor=CENTER, width=400)
        list_box_find.heading("# 5", text="اطلاعات بیشتر")
        list_box_find.place(x=3, y=200)

        by_name = IntVar()
        r1 = Checkbutton(win_show_factor, text="جستجو با نام مشتری", variable=by_name)
        by_code = IntVar()
        r1.place(x=10,y=10)
        r2 = Checkbutton(win_show_factor, text="جستجو با کد فاکتور", variable=by_code)
        by_date = IntVar()
        r2.place(x=10, y=40)
        r3 = Checkbutton(win_show_factor, text="جستجو با تاریخ", variable=by_date)
        r3.place(x=10, y=70)
        by_info = IntVar()
        r4 = Checkbutton(win_show_factor, text="جستجو با اطلاعات", variable=by_info)
        r4.place(x=10, y=100) 


        
        def search():  
            for item in list_box_find.get_children():
                    list_box_find.delete(item)
            list_items = []
            list_file = listdir(r"G:\check file\python\Accounting\factors")
            for j in list_file:
                file_csv = "G:/check file/python/Accounting/factors/{}".format(j)
                with open(file_csv, encoding="utf-8") as Rfile_csv:
                    read_csv = csv.reader(Rfile_csv)
                    x = 0
                    for i in read_csv:
                        if x == 0:
                            name_moshtari = i[0]
                            Purchase_code = i[1]
                            tarikhe_buy = i[2]
                            price_all = i[3]
                        elif x == 1:
                            more_info = i[0]
                            poole_naghd = i[1]
                            poole_kart = i[2]
                            poole_nasiye = i[3]
                        else:
                            list_items.insert(len(list_items), i)
                        x +=1
                    name_check = name_moshtari.strip().find(entry_name_factor.get().strip())


                    info_check = -1
                    for i in more_info.split():
                        if i.find(entry_info_factor.get("1.0", END).strip()) != -1:
                            info_check = 0

                    code_check = Purchase_code.strip().find(entry_code_factor.get().strip())

                    date_check = -1
                    date_factor = tarikhe_buy[:tarikhe_buy.find(" ")]
                    date_find = date(int(combo_y.get()), int(combo_m.get()), int(combo_d.get()))
                    if str(date_find) == date_factor :
                        date_check = 0

                    d=1
                    c=1
                    n=1
                    f=1
                    print(by_code.get(),by_date.get(),by_info.get(),by_name.get())
                    if by_date.get():
                        d = 2
                    if by_code.get():
                        c = 3
                    if by_name.get():
                        n = 4
                    if by_info.get():
                        f = 5
                    j = d * c * n * f
                    if (j == 5) and (info_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))
                    if (j == 4) and (name_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))
                    if (j == 3) and (code_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))
                    if (j == 2) and (date_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))

                    if (j == 6) and (date_check != -1) and (code_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))

                    if (j == 8) and (date_check != -1) and (name_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))
                    
                    if (j == 10) and (info_check != -1) and (date_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))

                    if (j == 12) and (name_check != -1) and (code_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))
                    
                    if (j == 15) and (info_check != -1) and (code_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))

                    if (j == 20) and (info_check != -1) and (name_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))

                    if (j == 24) and (date_check != -1) and (code_check != -1) and (name_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))

                    if (j == 30) and (date_check != -1) and (code_check != -1) and (info_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))
                    
                    if (j == 40) and (date_check != -1) and (name_check != -1) and (info_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))
                    
                    if (j == 60) and (code_check != -1) and (name_check != -1) and (info_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))

                    if (j == 120) and (date_check != -1) and (name_check != -1) and (info_check != -1) and (code_check != -1):
                        list_box_find.insert("",END, values=([name_moshtari, Purchase_code, tarikhe_buy, price_all, more_info]))

        

                        



                        
        
        
        
        
        
        
        
        Button(win_show_factor, text="search", font=("",16), command=search).place(x=30,y=130)
        
        win_show_factor.mainloop()




















    def setting_app(self, x):
        with open("setting.csv", encoding="utf-8") as setting_file:
            read_setting = csv.reader(setting_file)
            for i in read_setting:
                print(i)
                if x == 0:
                    return i[1]
                elif x == 1:
                    if i[0] == "code_buy_end":
                        return i[1]

    def code_buy(self):
        now_code = self.setting_app(1)
        code = int(now_code) + 1
        with open("setting.csv", "w", encoding="utf-8", newline='') as setting_file:
            write_setting = csv.writer(setting_file)
            write_setting.writerows([["version", "1.0"],["code_buy_end", code]])
            
        return code
        
    def factor_new(self):
        
        self.win_factor = Tk()
        self.win_factor.title("factor")
        self.win_factor.minsize(1000, 600)
        Label(self.win_factor, text="نام مشتری", font=("",16)).place(x=820, y=8)
        self.text_name_moshtari_input = Entry(self.win_factor, font=("",13), width=30)
        self.text_name_moshtari_input.place(x=540, y=10)

        Label(self.win_factor, font=("",14), text="نام محصول").place(x=230, y=30)
        self.text_name_item_input = Entry(self.win_factor, font=("",13), width=23)
        self.text_name_item_input.place(x=160, y=60)

        Label(self.win_factor, font=("",14), text="تعداد/متر").place(x=380, y=30)
        self.text_count_item_input = Entry(self.win_factor, font=("",13), width=5)
        self.text_count_item_input.place(x=390, y=60)

        Label(self.win_factor, font=("",14), text="قیمت واحد").place(x=465, y=30)
        self.text_price_item_input = Entry(self.win_factor, font=("",13), width=10)
        self.text_price_item_input.place(x=458, y=60)
        self.text_price_item_input.insert(0, "0")

        

        self.text_price_item = Label(self.win_factor, text= "", font=("",10))
        self.text_price_item.place(x=428, y=85)

        Label(self.win_factor, text="اطلاعات بیشتر فاکتور", font=("",16)).place(x=790,y=130)
        self.text_info_more = Text(self.win_factor, width=25, height=18, font=("", 13))
        self.text_info_more.place(x=750, y=175)        

        tree = ttk.Treeview(self.win_factor, column=("c1", "c2", "c3","c4"), show='headings', height=19)

        tree.column("# 1", anchor=CENTER,width=220)
        tree.heading("# 1", text="نام محصول")
        tree.column("# 2", anchor=CENTER, width=70)
        tree.heading("# 2", text="تعداد/متر")
        tree.column("# 3", anchor=CENTER, width=110)
        tree.heading("# 3", text="قیمت واحد")
        tree.column("# 4", anchor=CENTER, width=150)
        tree.heading("# 4", text="مجموع قیمت محصول")
 
        tree.place(x=160, y=120)

        self.my_list = Listbox(self.win_factor, height=23)     
        self.my_list.place(x=20, y=50)   

        self.text_name_item_total = Label(self.win_factor, text= "مجموع    :", font=("",20))
        self.text_name_item_total.place(x=150, y=535)
        Label(self.win_factor, text=":", font=("",20)).place(x=380, y=527)
        self.text_count_item_total = Label(self.win_factor, text= "0", font=("",16))
        self.text_count_item_total.place(x=390, y=535)
    
    
        self.text_price_item_total = Label(self.win_factor, text= "0", font=("",16))
        self.text_price_item_total.place(x=615, y=535)
        Label(self.win_factor, text=":", font=("",20)).place(x=565, y=527)

        self.text_price_item_total_text = Label(self.win_factor, text= "", font=("",13))
        self.text_price_item_total_text.place(x=460, y=560)
        self.price_total = 0
        self.count_total = 0
        self.list_item =[]

        def update(dete):
            try:
                self.text_price_item.configure(text=convert_ordinary(int(self.text_price_item_input.get())))
            except:
                self.text_price_item.configure(text="")
            self.my_list.delete(0,END)
            for item in dete:
                self.my_list.insert(0, item)
        def fillout(e):
            self.text_name_item_input.delete(0, END)
            self.text_name_item_input.insert(0, self.my_list.get(ACTIVE))
        def check(e):
            typed= self.text_name_item_input.get()
            if typed == "":
                dete = toppings
            else:
                dete= []
                for item in toppings:
                    if typed.lower() in item.lower():
                        dete.append(item)
            update(dete)
        toppings = ["زانو سفید", "زانو سفید 2.5", "زانو سفید 3", "سراه سفید", "سراه سفید 2.5", "سراه سفید 3"]
        update(toppings)
        self.my_list.bind("<<ListboxSelect>>",  fillout)
        self.text_name_item_input.bind("<KeyRelease>",  check)
        self.text_price_item_input.bind("<KeyRelease>",  check)

        def delete_item():
            tree.delete(tree.selection()[0])
            self.list_item = []
            for child in tree.get_children():
                self.list_item.insert(0, tree.item(child)["values"])
            print(self.list_item)
            self.price_total = 0
            self.count_total = 0
            for i in range(0, len(self.list_item)):
                self.count_total =self.count_total+ float(self.list_item[i][1])
                self.price_total = self.price_total+ int(float(self.list_item[i][3]))
           
            self.text_count_item_total.configure(text=self.count_total)
            self.text_price_item_total.configure(text=self.price_total)
            self.text_price_item_total_text.configure(text=convert_ordinary(self.price_total))

        def edit_item():
            xitem = tree.selection()[0]
            h_info = tree.item(xitem,["values"].pop())
            win_edit = Toplevel()
            win_edit.title("تغییر {} به تعداد {} و مبلغ {}".format(h_info[0], h_info[1], h_info[2]))
            win_edit.minsize(400, 250)
            def ok_edit():
                tree.item(xitem, values=(entry_name_item.get(), entry_count_item.get(), entry_price_item.get(), float(entry_count_item.get())* int(entry_price_item.get())))
                self.list_item = []
                for child in tree.get_children():
                    self.list_item.insert(0, tree.item(child)["values"])
                self.price_total = 0
                self.count_total = 0
                for i in range(0, len(self.list_item)):
                    self.count_total =self.count_total+ float(self.list_item[i][1])
                    self.price_total = self.price_total+ int(float(self.list_item[i][3]))
                self.text_count_item_total.configure(text=self.count_total)
                self.text_price_item_total.configure(text=self.price_total)
                self.text_price_item_total_text.configure(text=convert_ordinary(self.price_total))
                win_edit.destroy()
            Label(win_edit, text="نام محصول", font=("", 16)).place(x=80, y=10)
            entry_name_item = Entry(win_edit, font=("", 16))
            entry_name_item.place(x=20, y=40)
            Label(win_edit, text="تعداد/متر", font=("", 16)).place(x=80, y=90)
            entry_count_item = Entry(win_edit, font=("", 16))
            entry_count_item.place(x=20, y=120)
            Label(win_edit, text="قیمت واحد", font=("", 16)).place(x=80, y=160)
            entry_price_item = Entry(win_edit, font=("", 16))
            Button(win_edit, text="تایید", font=("",20), command=ok_edit, width=5).place(x=290, y=100)
            entry_price_item.place(x=20, y=190)

            

            win_edit.mainloop()


        def add_item2list(event=None):
            self.price_total = 0
            self.count_total = 0
                
            try:
                price_all = int(self.text_price_item_input.get())*float(self.text_count_item_input.get())
                tree.insert("", len(self.list_item), values=(self.text_name_item_input.get(), self.text_count_item_input.get(), self.text_price_item_input.get(),int(price_all)))
                self.list_item.insert(len(self.list_item), (self.text_name_item_input.get(), self.text_count_item_input.get(), self.text_price_item_input.get(), int(price_all)))
                
                
                self.text_price_item.configure(text=convert_ordinary(int(self.text_price_item_input.get())))


                print(self.list_item)
                
                
                for i in range(0, len(self.list_item)):
                    self.count_total =self.count_total+ float(self.list_item[i][1])
                    self.price_total = self.price_total+ int(float(self.list_item[i][3]))
                self.text_count_item_total.configure(text=self.count_total)
                self.text_price_item_total.configure(text=self.price_total)
                self.text_price_item_total_text.configure(text=convert_ordinary(self.price_total))
                self.text_name_item_input.delete(0,END)
                self.text_name_item_input.focus()
                if self.price_total != 0:
                    self.btn_accept_item_end.configure(state="active")
            except:
                messagebox.showerror("Error", "لطفا ورودی ها را صحیح وارد کنید")
                
        self.price_naghd_end= 0
        self.price_nasiye_end= 0
        self.price_kart_end = 0

        def accept_end():
            win_factor_end = Toplevel(self.win_factor)
            win_factor_end.title("Pay")
            win_factor_end.minsize(600, 300)
            win_factor_end.maxsize(600, 300)
            
            self.amount_paid = self.price_total
            def chortke(event=None):
 
                if input_naghd.get() == "":
                    input_naghd.insert(0,"0")
                elif input_naghd.cget("state")== "normal":
                    print("01",input_naghd.cget("state"))
                    self.price_naghd_end = int(input_naghd.get()) + self.price_naghd_end
                    input_naghd.configure(state="disabled")
                    self.amount_paid = self.amount_paid - self.price_naghd_end
                if input_kart.get() == "":
                    input_kart.insert(0,"0")
                elif input_kart.cget("state")== "normal":
                    print("02",input_kart.cget("state"))
                    self.price_kart_end = int(input_kart.get())+ self.price_kart_end
                    input_kart.configure(state="disabled")
                    self.amount_paid = self.amount_paid - self.price_kart_end
                if input_nasiye.get() == "":
                    input_nasiye.insert(0,"0")
                elif input_nasiye.cget("state")== "normal":
                    print("03",input_nasiye.cget("state"))
                    self.price_nasiye_end = int(input_nasiye.get()) + self.price_nasiye_end
                    input_nasiye.configure(state="disabled")
                    self.amount_paid = self.amount_paid - self.price_nasiye_end

                
                text_price_end.configure(text=self.amount_paid)
                text_price_end_tx.configure(text=convert_ordinary(int(self.amount_paid)))
                
                if text_naghd.cget("text") == "":
                    text_naghd.configure(text=convert_ordinary(int(input_naghd.get())))
                if text_kart.cget("text") == "":
                    text_kart.configure(text=convert_ordinary(int(input_kart.get())))
                if text_nasiye.cget("text") == "":
                    text_nasiye.configure(text=convert_ordinary(int(input_nasiye.get())))

                input_nasiye.delete(0,END)
                input_kart.delete(0,END)
                input_naghd.delete(0,END)
                if self.amount_paid == 0:
                    btn_taeed.configure(state="active")

            def space(event= None):
                copy(self.amount_paid)
                input_naghd.focus_get().insert(0, paste())
                
                
            def save_factor():
                try:
                    self.tarikh = datetime.now()
                    print(self.tarikh)
                    code = self.code_buy()
                    with open("factors/factor_{}.csv".format(code), "w", encoding="utf-8",newline='') as csv_save:
                        csv_write = csv.writer(csv_save)
                        csv_write.writerow([self.text_name_moshtari_input.get(), code, self.tarikh, self.price_total])
                        csv_write.writerow([ self.text_info_more.get("1.0", END), self.price_naghd_end, self.price_kart_end, self.price_nasiye_end])
                        for child in tree.get_children():
                            print(tree.item(child)["values"])
                            csv_write.writerow(tree.item(child)["values"])
                    messagebox.showinfo("save_compelect", "فاکتور با کد {} ذخیره شد".format(code))
                    win_factor_end.destroy()
                    self.win_factor.destroy()
                except:
                    messagebox.showerror("Error", "!!مشکلی رخ داد فاکتور ذخیره نشد")
                
                
            win_factor_end.bind("<Return>",chortke)
            win_factor_end.bind("<space>",space)
            btn_taeed = Button(win_factor_end, text="اوکی", font=("",13), state="disabled", command=save_factor)
            btn_taeed.place(x=380, y=60)
            text_price_end = Label(win_factor_end, text=self.price_total, font=("", 16))
            text_price_end.place(x=110, y=220)
            text_price_end_tx = Label(win_factor_end, text=convert_ordinary(int(self.price_total)), font=("", 16))
            text_price_end_tx.place(x=110, y=260)
            Label(win_factor_end, text="دریافت شود:", font=("", 16)).place(x=10, y=220)
            
            btn_naghd = Button(win_factor_end, text="نقدی", font=("",16), command=chortke)
            btn_naghd.place(x=40, y=20)
            input_naghd = Entry(win_factor_end, font=("",16), width=10)
            input_naghd.place(x=110,y=25)
            text_naghd = Label(win_factor_end, text="", font=("",13))
            text_naghd.place(x=240, y=26)

            btn_kart = Button(win_factor_end, text="کارتخوان", font=("",16), command=chortke)
            btn_kart.place(x=10, y=80)
            input_kart = Entry(win_factor_end, font=("",16), width=10)
            input_kart.place(x=110,y=85)
            text_kart = Label(win_factor_end, text="", font=("",13))
            text_kart.place(x=240, y=86)

            btn_nasiye = Button(win_factor_end, text="نسیه", font=("",16), command=chortke)
            btn_nasiye.place(x=45, y=140)
            input_nasiye = Entry(win_factor_end, font=("",16), width=10)
            input_nasiye.place(x=110,y=145)
            text_nasiye = Label(win_factor_end, text="", font=("",13))
            text_nasiye.place(x=240, y=146)
            

            win_factor_end.mainloop()

        self.btn_accept_item = Button(self.win_factor, text="تایید", font=("",16), command=add_item2list, width=10)
        self.btn_accept_item.place(x=760, y=50)
        self.win_factor.bind("<Return>", add_item2list)

        self.btn_delete_item = Button(self.win_factor, text="delete", font=("",16), command=delete_item, width=10)
        self.btn_delete_item.place(x=15, y=430)

        self.btn_delete_item = Button(self.win_factor, text="edit", font=("",16), command=edit_item, width=10)
        self.btn_delete_item.place(x=15, y=480)

        self.btn_accept_item_end = Button(self.win_factor, text="تایید نهایی", font=("",16), state="disabled", command=accept_end, width=10)
        self.btn_accept_item_end.place(x=810, y=540)

        self.win_factor.mainloop()

    
        

run=Program()
acw1668
  • 40,144
  • 5
  • 22
  • 34
gamerfan82
  • 33
  • 5
  • 1
    It is because you have used multiple instances of `Tk()`. Use `Toplevel()` for windows other than the main/root window. – acw1668 Feb 03 '23 at 10:08

0 Answers0