Python Forum
GUI and special listbox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GUI and special listbox
#1
Try to make a GUI for one application I'am working on. Try Tkinter, but I can´t handle a While-loop and reciving UDP-messages at the same time I use Tkinter as GUI. Maybe I can start different threads, but am relatively new to Python. Have a decent grasp of the python code how it is written (maybe not the best coder) until I get to the graphical stuff. Think

Try Pygame just for the GUI, I can recive UDP-message but I don´t find the classic HTML-listbox <SELECT> in Pygame.

Is there another GUI to look at with listbox?

What I'm looking for is to have a fixed page (1024x768) where I have a few different labels, some buttons and a large "listbox". I want to be able to change the text of the labels at any time in my code. They should never move, only change content

* In the listbox, I would like to have each row with one "label" and two "input".
* I would like to be able to receive a UDP command and display the current row in the middle of the list box + 10 lines before and 10 lines after. So I need to set the height for my listbox and select which rows to view.
* I want to be able to scroll in the list box on the screen when I don´t recive UDP-commands that says jump to row.
* I want to save the content in my listbox to a file.

I don´t wan´t someone to do this for me... But I need to know if someone had seen this before and know some suitable GUI.

I am relatively new to Python but have done some programming before. When searching for Python and GUI, it is mostly Tkinter that appears in my searches. Sad

EDIT:
Can also imagine if it is possible to make a classic HTML-"<table>" with 21 rows and 3 columns. If it was possible to add these 21 lines of type:

for in to range(21)
 hiddenValue = add.hidden(value=myArrayRow)
 label = add.label(text="text", column="0")
 input = add.input(text="text", column="1")
 input2 = add.input2(text="text", column="2")
...to then be able to update label[x] = "new text" whitout redrawing entire page and then retrieve the value from input[x] and input2[x] to save in an array which is then saved later in a text file.
buran write Nov-28-2024, 01:50 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Note you code snippet is NOT syntax correct
Reply
#2
Sorry for that Buran. Why I don´t use the BBCode-part is that I don´t have the proper code. I just wrote a "dummy"-code to explain what I would do and how I could solve this in VB6 or in .NET... I don´t need help to do the code, just getting help to found a GUI that give me some help to solve my problem. If I use "proper" code I probably get answer that I can't do that isntead of answer my question about a GUI I could use.

After spending some time on Google it looks like PYsimpleGUI could help me with my problem.
Reply
#3
Quote:Try Tkinter, but I can´t handle a While-loop and reciving UDP-messages at the same time
You will have the same problem with any GUI toolkit that uses an event loop to processes events. Even pygame. The solution is to put the UDP message receiving code in a different thread or write the code so it doesn't block. If you can write the UDP receiving code so it doesn't block, use the .after(time, func) method in tkinter to periodically call the UDP code.
MacTommy likes this post
Reply
#4
(Nov-29-2024, 09:04 PM)deanhystad Wrote:
Quote:Try Tkinter, but I can´t handle a While-loop and reciving UDP-messages at the same time
You will have the same problem with any GUI toolkit that uses an event loop to processes events. Even pygame. The solution is to put the UDP message receiving code in a different thread or write the code so it doesn't block. If you can write the UDP receiving code so it doesn't block, use the .after(time, func) method in tkinter to periodically call the UDP code.

Thanks Deanhystad. Found out even PYsimpleGUI has issues for me, but I had the possibility to work this around with timeout...
event, values = window.Red(timeout=150)
Afterwards I found out that 2.7 of PYsimpleGUI get me some error when I try to update a text-box. Later version of PYsimpleGUI has other issues with some other function I use in my code.

Try to make a thread and go back to TKinter or PYgame.
Reply
#5
If you don't use a thread, your receive socket timeout should be 0, not 150. I would set the receiving socket to be non-blocking and catch the blocking error.
import socket
import tkinter as tk


UDP_IP = "127.0.0.1"
UDP_PORT = 5005


def send_text(*_):
    text = send_text_var.get()
    send_sock.sendto(text.encode(), (UDP_IP, UDP_PORT))


def recv_text():
    try:
        text, _ = recv_sock.recvfrom(1024)
        if text:
            recv_text_var.set(text.decode())
    except BlockingIOError:
        pass
    root.after(100, recv_text)


send_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
recv_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
recv_sock.bind((UDP_IP, UDP_PORT))
recv_sock.setblocking(False)

root = tk.Tk()
send_text_var = tk.StringVar()
tk.Entry(root, textvariable=send_text_var, width=30).pack()
recv_text_var = tk.StringVar()
tk.Label(root, textvariable=recv_text_var).pack()
tk.Button(root, text="Send", command=send_text).pack()

recv_text()
root.mainloop()
Note: This code runs on windows 10 using python 3.10.
MacTommy likes this post
Reply
#6
(Dec-02-2024, 04:00 PM)deanhystad Wrote: If you don't use a thread, your receive socket timeout should be 0, not 150. I would set the receiving socket to be non-blocking and catch the blocking error.
import socket
import tkinter as tk


UDP_IP = "127.0.0.1"
UDP_PORT = 5005


def send_text(*_):
    text = send_text_var.get()
    send_sock.sendto(text.encode(), (UDP_IP, UDP_PORT))


def recv_text():
    try:
        text, _ = recv_sock.recvfrom(1024)
        if text:
            recv_text_var.set(text.decode())
    except BlockingIOError:
        pass
    root.after(100, recv_text)


send_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
recv_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
recv_sock.bind((UDP_IP, UDP_PORT))
recv_sock.setblocking(False)

root = tk.Tk()
send_text_var = tk.StringVar()
tk.Entry(root, textvariable=send_text_var, width=30).pack()
recv_text_var = tk.StringVar()
tk.Label(root, textvariable=recv_text_var).pack()
tk.Button(root, text="Send", command=send_text).pack()

recv_text()
root.mainloop()
Note: This code runs on windows 10 using python 3.10.

Changed it to threads instead... because of problem with timing. :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020