Questions Python (tkinter/GUI) වැඩ්ඩො

tom2pd

Well-known member
  • Dec 22, 2007
    913
    115
    63
    Place in your Hart
    මචං මේකයි කතන්දරේ
    Module දෙකක් තියෙනවා එකක GUI class 1 තියෙනවා
    අනිත් module එකේ තියෙන variable එක update වුනොත් GUI ක්ලාස් එකෙ තියෙන list box එකට add වෙන්න ඕන

    දන්න කෙනෙක් sup එකක් දෙන්න


    GUI.py
    Python:
    from tkinter import *
    import time
    
    # def main():
    #     root = Tk()
    #     window1 = Window(root)
    #     # window1.insLi('00')
    
    class Window:
    
    
        def __init__(self,root,li) -> None:
            self.li = li
            self.root = root
    
        def main(self):
            # Creating the root window
    
            self.root.geometry("609x96+951+216")
            self.root.minsize(120, 1)
            self.root.maxsize(2052, 1133)
            self.root.resizable(1,  1)
            self.root.title("Signal")
            self.root.configure(background="#d9d9d9")
            self.root.configure(highlightbackground="#d9d9d9")
            self.root.configure(highlightcolor="black")
    
            # Creating a Listbox and
            # attaching it to root window
            listbox = Listbox(self.root)
            self.lb = listbox
            # Adding Listbox to the left
            # side of root window
            listbox.pack(side=LEFT, fill=BOTH)
    
            # Creating a Scrollbar and
            # attaching it to root window
            scrollbar = Scrollbar(self.root)
    
            # Adding Scrollbar to the right
            # side of root window
            scrollbar.pack(side=RIGHT, fill=BOTH)
    
            # Insert elements into the listbox
            # for values in range(100):
    
            listbox.config(yscrollcommand=scrollbar.set)
            listbox.place(relx=0.0, rely=0.0, relheight=1.0, relwidth=1.0)
            listbox.configure(background="#000000")
            listbox.configure(disabledforeground="#6d6d6d")
            listbox.configure(font="-family {Segoe UI Semibold} -size 18 -weight bold")
            listbox.configure(foreground="#04ff24")
            listbox.configure(highlightbackground="#d9d9d9")
            listbox.configure(highlightcolor="black")
            listbox.configure(selectbackground="blue")
            listbox.configure(selectforeground="white")
    
    
            # listbox.selection_set(END)
            # listbox.see(END)
    
            scrollbar.config(command=listbox.yview)
            
            # self.root.after(1000,insLi)
            self.root.mainloop()
            
    
    
        def insLi(self):
    
            # while True:
            if self.li != '':
                self.lb.selection_clear(0,END)
                self.lb.insert(END, self.li)
                self.lb.selection_set(END)
                self.lb.see(END)
                time.sleep(1)

    updater.py
    Python:
    from tkinter import *
    import GUI as sf
    
    
    def popupmsg():
        root = Tk()
        window1 = sf.Window(root,'')
        t=th.Thread(target=window1.main)
        t.start()
    
    popupmsg()
    sf.window1.li='update me'
     
    • Like
    Reactions: JohnSnow and kinkon

    0x9

    Well-known member
  • Mar 18, 2015
    541
    836
    93
    python ගැන නම් ලොකුවට දන්නේ නෑ, string එකකට වඩා queue එකක් use කරොත් වැඩේ ලේසි වෙයි නේද?

    python.png


    modified code එක

    updater.py

    Python:
    from tkinter import *
    import threading, queue
    import GUI as sf
    c = threading.Condition()
    
    def popupmsg(q):
        root = Tk()
        window = sf.Window(root,q)
        window.main()
    
    q = queue.Queue()
    t = threading.Thread(target=popupmsg,args=(q,))
    t.start()
    data = ["apple", "banana", "cherry"]
    for x in data:
      q.put(x)


    GUI.py

    Python:
    from tkinter import *
    import time
    import threading
    
    # def main():
    #     root = Tk()
    #     window1 = Window(root)
    #     # window1.insLi('00')
    
    class Window:
    
    
        def __init__(self,root,queue) -> None:
            self.queue = queue
            self.root = root
            
    
    
        def main(self):
            # Creating the root window
    
            self.root.geometry("609x96+951+216")
            self.root.minsize(120, 1)
            self.root.maxsize(2052, 1133)
            self.root.resizable(1,  1)
            self.root.title("Signal")
            self.root.configure(background="#d9d9d9")
            self.root.configure(highlightbackground="#d9d9d9")
            self.root.configure(highlightcolor="black")
    
            # Creating a Listbox and
            # attaching it to root window
            listbox = Listbox(self.root)
            self.lb = listbox
            # Adding Listbox to the left
            # side of root window
            listbox.pack(side=LEFT, fill=BOTH)
    
            # Creating a Scrollbar and
            # attaching it to root window
            scrollbar = Scrollbar(self.root)
    
            # Adding Scrollbar to the right
            # side of root window
            scrollbar.pack(side=RIGHT, fill=BOTH)
    
            # Insert elements into the listbox
            # for values in range(100):
    
            listbox.config(yscrollcommand=scrollbar.set)
            listbox.place(relx=0.0, rely=0.0, relheight=1.0, relwidth=1.0)
            listbox.configure(background="#000000")
            listbox.configure(disabledforeground="#6d6d6d")
            listbox.configure(font="-family {Segoe UI Semibold} -size 18 -weight bold")
            listbox.configure(foreground="#04ff24")
            listbox.configure(highlightbackground="#d9d9d9")
            listbox.configure(highlightcolor="black")
            listbox.configure(selectbackground="blue")
            listbox.configure(selectforeground="white")
    
    
            # listbox.selection_set(END)
            # listbox.see(END)
    
            scrollbar.config(command=listbox.yview)
            
            def update_list():
                while True:
                    data = self.queue.get()
                    self.lb.selection_clear(0,END)
                    self.lb.insert(END, data)
                    self.lb.selection_set(END)
                    self.lb.see(END)
            
            thread = threading.Thread(target=update_list, daemon=True)
            thread.start()
            
            # self.root.after(1000,insLi)
            self.root.mainloop()
     
    • Love
    • Like
    Reactions: tom2pd and JohnSnow

    tom2pd

    Well-known member
  • Dec 22, 2007
    913
    115
    63
    Place in your Hart
    python ගැන නම් ලොකුවට දන්නේ නෑ, string එකකට වඩා queue එකක් use කරොත් වැඩේ ලේසි වෙයි නේද?

    View attachment 154350

    modified code එක

    updater.py

    Python:
    from tkinter import *
    import threading, queue
    import GUI as sf
    c = threading.Condition()
    
    def popupmsg(q):
        root = Tk()
        window = sf.Window(root,q)
        window.main()
    
    q = queue.Queue()
    t = threading.Thread(target=popupmsg,args=(q,))
    t.start()
    data = ["apple", "banana", "cherry"]
    for x in data:
      q.put(x)


    GUI.py

    Python:
    from tkinter import *
    import time
    import threading
    
    # def main():
    #     root = Tk()
    #     window1 = Window(root)
    #     # window1.insLi('00')
    
    class Window:
    
    
        def __init__(self,root,queue) -> None:
            self.queue = queue
            self.root = root
          
    
    
        def main(self):
            # Creating the root window
    
            self.root.geometry("609x96+951+216")
            self.root.minsize(120, 1)
            self.root.maxsize(2052, 1133)
            self.root.resizable(1,  1)
            self.root.title("Signal")
            self.root.configure(background="#d9d9d9")
            self.root.configure(highlightbackground="#d9d9d9")
            self.root.configure(highlightcolor="black")
    
            # Creating a Listbox and
            # attaching it to root window
            listbox = Listbox(self.root)
            self.lb = listbox
            # Adding Listbox to the left
            # side of root window
            listbox.pack(side=LEFT, fill=BOTH)
    
            # Creating a Scrollbar and
            # attaching it to root window
            scrollbar = Scrollbar(self.root)
    
            # Adding Scrollbar to the right
            # side of root window
            scrollbar.pack(side=RIGHT, fill=BOTH)
    
            # Insert elements into the listbox
            # for values in range(100):
    
            listbox.config(yscrollcommand=scrollbar.set)
            listbox.place(relx=0.0, rely=0.0, relheight=1.0, relwidth=1.0)
            listbox.configure(background="#000000")
            listbox.configure(disabledforeground="#6d6d6d")
            listbox.configure(font="-family {Segoe UI Semibold} -size 18 -weight bold")
            listbox.configure(foreground="#04ff24")
            listbox.configure(highlightbackground="#d9d9d9")
            listbox.configure(highlightcolor="black")
            listbox.configure(selectbackground="blue")
            listbox.configure(selectforeground="white")
    
    
            # listbox.selection_set(END)
            # listbox.see(END)
    
            scrollbar.config(command=listbox.yview)
          
            def update_list():
                while True:
                    data = self.queue.get()
                    self.lb.selection_clear(0,END)
                    self.lb.insert(END, data)
                    self.lb.selection_set(END)
                    self.lb.see(END)
          
            thread = threading.Thread(target=update_list, daemon=True)
            thread.start()
          
            # self.root.after(1000,insLi)
            self.root.mainloop()
    අඩෝ උබනම් දෙය්යෙක් නෙමෙයි දේවාල ගෙනියන නැවක්
    තැන්ක්ස් මචන්
     
    • Like
    Reactions: 0x9