The button goes under the search frame, Tkinter

I trying to do a search frame, but when I added the button, It goes under the frame of the search, appears, and then disappears fast. In the search entry, when I run the code, it appears, and when I run it a second time, it disappears.

the output

I’m trying to do a simple origination desktop app; any advice would be very helpful.

from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk
from datetime import datetime
import os, sys, shutil
from tkcalendar import Calendar, DateEntry
from threading import Thread
import time as t


class WidgetsApp:
    def __init__(self, master):
        self.master = master
        self.master.geometry("1200x900+350+40")
        self.master.resizable(width=False, height=False)
        self.master.title("نظام أدارة تجمع العلماء")
        
        self.call_fuction_add_title_frame = Thread(target=self.add_frame_title, args=())
        self.call_fuction_add_title_frame.start()

        self.call_add_search_frame = Thread(target=self.add_search_frame, args=())
        self.call_add_search_frame.start()

        

        self.call_search_person = Thread(target=self.add_entry_search, args=())
        self.call_search_person.start()
        self.call_button_search = Thread(target=self.get_search,args=())
        self.call_button_search.start()
        

    def add_frame_title(self):
        self.title_frame = Frame(
            self.master,
            width=1175,
            height=80,
            background="#E8E4FA",
        )
        self.title_frame.place(x=18, y=4)
        self.title_label = Label(
            self.title_frame,
            width=30,
            height=1,
            background="#E8E4FA",
            font=("Libre Baskerville, serif;", 26),
            text="أضافة عالم",
        )
        self.title_label.place(x=300, y=20)

    def add_search_frame(self):
        self.search_frame = Frame(
            self.master,
            width=1165,
            height=80,
            background="#d9ddf7",
            relief="solid",
            border=2,
        )
        self.search_frame.place(x=18, y=88)

        self.search_label = Label(
            self.master,
            background="#d9ddf7",
            text="أبحث عن أسم",
            font=("Bold Oblique", 18),
        )
        self.search_label.place(x=1060, y=113)

    def add_entry_search(self):
        self.search_person = Entry(
            self.master,
            width=20,
            background="powder blue",
            font=("Bold Oblique", 18),
            relief=RIDGE,
            border=1,
        )
        self.search_person.place(x=200, y=113)

    def get_search(self):
        self.image_search = Image.open("images\user-avatar.png")
        self.image_search = self.image_search.resize((35, 35))
        self.show_image_search = ImageTk.PhotoImage(self.image_search)
        self.button_search = Button(
            self.master,
            width=90,
            height=30,
            text="أبحث",
            font=("Bold Oblique", 18),
            padx=20,
            relief=FLAT,
            bd=1,
            compound='left',
            image=self.show_image_search
        )
        self.button_search.place(x=40,y=106)

3

Your code have severe problems with your program structure, so I basically rewrote the structure and control flow while retaining what you want. Let me first list two major issues with the code:

  1. No need to use threads. Threads are used in much, much more complex scenarios than this, and the use of threads here makes no sense at all. It just complicates the code. Also, I’ve rarely seen any tkinter code with threads in use.
  2. You didn’t call the mainloop() in your class, nor any of the methods that are supposed to place widgets on the screen (i.e., add_frame_title, add_search_frame, add_entry_search, …). Either the code given is incomplete, or you didn’t ever call them. If appears (at least to me) that you call the mainloop() and all the methods that creates the widgets outside of your class, which is not a good practice in writing real-world complex tkinter GUI programs.

Anyway, here’s the code:

from tkinter import ttk
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
from datetime import datetime
import os, sys, shutil
from tkcalendar import Calendar, DateEntry
import time as t


class WidgetsApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.geometry("1200x900+350+40")
        self.resizable(width=False, height=False)
        self.title("نظام أدارة تجمع العلماء")
        self.add_frame_title()
        self.add_search_frame()
        self.add_entry_search()
        self.get_search()

    def add_frame_title(self):
        self.title_frame = tk.Frame(
            self,
            width=1175,
            height=80,
            background="#E8E4FA",
            )
        self.title_frame.place(x=18, y=4)
        self.title_label = tk.Label(
            self.title_frame,
            width=30,
            height=1,
            background="#E8E4FA",
            font=("Libre Baskerville, serif;", 26),
            text="أضافة عالم",
            )
        self.title_label.place(x=300, y=20)

    def add_search_frame(self):
        self.search_frame = tk.Frame(
            self,
            width=1165,
            height=80,
            background="#d9ddf7",
            relief="solid",
            border=2,
            )
        self.search_frame.place(x=18, y=88)

        self.search_label = tk.Label(
            self,
            background="#d9ddf7",
            text="أبحث عن أسم",
            font=("Bold Oblique", 18),
            )
        self.search_label.place(x=1060, y=113)

    def add_entry_search(self):
        self.search_person = tk.Entry(
            self,
            width=20,
            background="powder blue",
            font=("Bold Oblique", 18),
            relief=tk.RIDGE,
            border=1,
            )
        self.search_person.place(x=200, y=113)

    def get_search(self):
        self.image_search = Image.open("images\user-avatar.png")
        self.image_search = self.image_search.resize((35, 35))
        self.show_image_search = ImageTk.PhotoImage(self.image_search)
        self.button_search = tk.Button(
            self,
            width=90,
            height=30,
            text="أبحث",
            font=("Bold Oblique", 18),
            padx=20,
            relief=tk.FLAT,
            bd=1,
            compound='left',
            image=self.show_image_search
            )
        self.button_search.place(x=40, y=106)


if __name__ == '__main__':
    app = WidgetsApp()
    app.mainloop()

Note that I also incorporated the common method when using tkinter on real-world projects: inherit tk.Tk() in your own class, so that it can be treated by users just like the original tk.Tk() but with customized and added functionality. I also changed the from tkinter import * and relevant names to prevent namespace corruption.

1

Since you used threads to create widgets, it is not guaranteed that those threads were executed in the same sequence as how they were started. So the search button might be created before the search frame and so was covered by the search frame.

Actually you don’t need (or should not) use threads to create widgets. Just call those methods in sequence and you will get what you want:

class WidgetsApp:
    def __init__(self, master):
        self.master = master
        self.master.geometry("1200x900+350+40")
        self.master.resizable(width=False, height=False)
        self.master.title("نظام أدارة تجمع العلماء")

        self.add_frame_title()
        self.add_search_frame()
        self.add_entry_search()
        self.get_search()

   ...

Result:

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật