Python Forum
Syntax error for "root = Tk()"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error for "root = Tk()"
#11
Yeah!!! It runs, after I made some changes to get the GUI window working. I added a few print statements to follow progress. Still fails to connect.

Some of the results:

****
Test button MAC: b'MAC\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

IP is: 192 . 168 . 0 . 168

UDP-IP is: 192.168.0.168

Test button device address: ('192.168.0.168', 50000)

message sent

message sent

message sent

message sent

message sent
*** and then I get the device not found message. Looks like either the socket is not working as expected or the UDP port has changed. I have a query out to a forum for the radio asking what is the default UDP address.

I'm puzzled about the single quotes around the IP address in the "device address". I do not see where they come from or even if they are a problem.

In the mean time, I'd like some input on setting the window, if you are not fed up with me yet. :)

Here is the unaltered code:

# ----------------------------------
#    User Interface
#------------------------------------
def close():
    sock.close()
    root.destroy()
    root.quit()
    
root=Tk()
root.title('Bootloader 2.1')
x = str((root.winfo_screenwidth() - root.winfo_reqwidth()) / 3)
y = str((root.winfo_screenheight() - root.winfo_reqheight()) / 3)
root.geometry('400x300+'+x+'+'+y)
root.maxsize(400, 330)
root.minsize(400, 330)
root.protocol('WM_DELETE_WINDOW', close)
The resulting error:

Error:
Traceback (most recent call last): File "E:\HamStuff\Odyssey\BL2.1\Bootloader2to3Results.pyw", line 24, in <module> root.geometry('400x300+'+x+'+'+y) File "C:\Program Files\Python312\Lib\tkinter\__init__.py", line 2114, in wm_geometry return self.tk.call('wm', 'geometry', self._w, newGeometry) _tkinter.TclError: bad geometry specifier "400x300+573.3333333333334+293.3333333333333"
My guess is that the intent is to place the GUI in a place dependent on the screen size. Also, I suspect the geometry specifier should be an integer, since it is pixels. I modified the code to have fixed values for "x" and "y" or 100 each. That put the GUI in the upper left of the screen.
Reply
#12
Quote:I'm puzzled about the single quotes around the IP address in the "device address". I do not see where they come from or even if they are a problem.
When printing, python calls str() for objects directly referenced in the print command, and repr() for objects that are parts of other objects (items in tuples or lists for example). The repr() function wraps the string in single quotes to tell the user that this is a str. You can see this here:
print("hello", ("hello",))
Output:
hello ('hello',)
Quote:In the mean time, I'd like some input on setting the window
This looks suspicious.
Output:
573.3333333333334+293.3333333333333
I bet those are supposed to be ints, not floats. I would forget about setting the window position. Let your computer decide where it should go.
Reply
#13
If you're just wanting to center the window

import tkinter as tk 

def close():
    ''' Will close the tkinter window '''
    root.destroy()

root = tk.Tk()

# Get the screen size
screensize = root.winfo_screenwidth(), root.winfo_screenheight()

# Calculate the center
x = screensize[0]//2-root.winfo_reqwidth()*2
y = screensize[1]//2-root.winfo_reqheight()*2

# Set the geometry
root.geometry(f'800x600+{x}+{y}')

# Window can't be resized
root.resizable(False, False)

label = tk.Label(root, text=f'X position = {x}, Y position = {y}')
label.pack(pady=20)


btn = tk.Button(root, text='Close', command=close, cursor='hand2')
btn.pack(side='bottom', padx=5, pady=5)

''' No need for this unless using a top level window 
    example would be to close toplevel window and deiconify main window '''
root.protocol('WM_DELETE_WINDOW', close)

root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#14
(Jan-28-2024, 06:47 PM)menator01 Wrote: If you're just wanting to center the window

import tkinter as tk 

def close():
    ''' Will close the tkinter window '''
    root.destroy()

root = tk.Tk()

# Get the screen size
screensize = root.winfo_screenwidth(), root.winfo_screenheight()

# Calculate the center
x = screensize[0]//2-root.winfo_reqwidth()*2
y = screensize[1]//2-root.winfo_reqheight()*2

# Set the geometry
root.geometry(f'800x600+{x}+{y}')

# Window can't be resized
root.resizable(False, False)

label = tk.Label(root, text=f'X position = {x}, Y position = {y}')
label.pack(pady=20)


btn = tk.Button(root, text='Close', command=close, cursor='hand2')
btn.pack(side='bottom', padx=5, pady=5)

''' No need for this unless using a top level window 
    example would be to close toplevel window and deiconify main window '''
root.protocol('WM_DELETE_WINDOW', close)

root.mainloop()

Centering is great. And it works for centering the window.

Thanks.

As for the inability to connect, the UDP port is supposed to be 50000, which is what the program uses. Could be that the radio is programmed incorrectly. My next step is to try to reprogram the radio.
Reply
#15
Do you have a network connection on your computer configured to have a static IP address on the 192.168.0 subnet? sock.sendto() quietly fails if it cannot send or nobody is listening. You don't know if the message was received until you get back a response, and your program is not getting a response.

If you want a smaller program to test with, this program just sends the MAC command and reads the response:
import socket


def empty_read_buffer(sock):
    try:
        while True:
            sock.recv(4096)
    except TimeoutError:
        pass


IP = '192.168.0.168'
PORT = 50000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(0.2)

empty_read_buffer(sock)
msg = b'MAC' + bytes([0]*29)   
for _ in range(5):
    sock.sendto(msg, (IP, PORT))
    try:
        reply = sock.recv(32)
        print(reply)
        if reply and reply[:3] == msg[:3]:
            break
    except TimeoutError:
        pass

else:
    print("Failed to read response.")
Reply
#16
(Jan-28-2024, 11:03 PM)deanhystad Wrote: Do you have a network connection on your computer configured to have a static IP address on the 192.168.0 subnet? sock.sendto() quietly fails if it cannot send or nobody is listening. You don't know if the message was received until you get back a response, and your program is not getting a response.

If you want a smaller program to test with, this program just sends the MAC command and reads the response:
import socket


def empty_read_buffer(sock):
    try:
        while True:
            sock.recv(4096)
    except TimeoutError:
        pass


IP = '192.168.0.168'
PORT = 50000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(0.2)

empty_read_buffer(sock)
msg = b'MAC' + bytes([0]*29)   
for _ in range(5):
    sock.sendto(msg, (IP, PORT))
    try:
        reply = sock.recv(32)
        print(reply)
        if reply and reply[:3] == msg[:3]:
            break
    except TimeoutError:
        pass

else:
    print("Failed to read response.")

I'll try the small program tomorrow.

My computer is on the same subnet as the radio, but uses DHCP. I checked its IP (192.168.0.14) to make sure both were on the same subnet. Computer is set to Gigabit speed. The radio only works on Gigabit Ethernet. Both are plugged into a Gigabit switch along with several other non-active things. I tried a direct connect between computer and radio a while back without success. The radio is supposed to work that way, but I have not tried to operate it in that configuration only to test the bootloader program.

I know the radio and computer work together because the software that runs the radio sees and controls the radio without any problems. I would like to be able to put the radio on a different subnet and to be able to update firmware. The Bootloader program will do both when I get it working.

I've forgotten most of my network knowledge. Do you know how to look at network traffic and see what UDP port the radio is actually using? There used to be programs that can be run on a computer to monitor network traffic. BTW, "arp -a" from a command window does not see the radio. That is not too surprising, because the radio is definitely not programmed to respond to "ping" and probably not "arp -a" either. The software that runs the radio uses a local broadcast to discover the radio.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo SYNTAX ERROR Yannko 3 401 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,627 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,242 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,327 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,273 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 911 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,872 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,378 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,987 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  Query Syntax Error hammer 2 1,630 Jan-03-2022, 02:30 PM
Last Post: hammer

Forum Jump:

User Panel Messages

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