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:
- 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.
- 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 themainloop()
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