0

I am new to Python. I try to use it to create a GUI, in order to control my arduino.

Since i don't fully understand python, much less Tkinter or pySerial, i found a snippet of code online.

The snippet is supposed to create a GUI, and also there will be an option to select the COM port, to communicate with the arduino.

This is the code:

import serial.tools.list_ports
from tkinter import *

def on_select(selection):
    # open the port and command it to start the LED blinking here
    print(selection)

root = Tk()
ports = serial.tools.list_ports.comports()
default = StringVar(root, "Please Select Port")
OptionMenu(root, default, *ports, command=on_select).pack()
root.mainloop()

I run this (i have Python 3.8.2) and this is the error i get:

Traceback (most recent call last):
  File "code.py", line 11, in <module>
    OptionMenu(root, default, *ports, command=on_select).pack()
TypeError: __init__() missing 1 required positional argument: 'value'

However, i do not have an init() in order to pass an argument.

2
  • you're calling it implicitly, with wrong / not enough positional parameters Commented Apr 5, 2020 at 20:14
  • '__ init __' is the initalization function of a class. It is automatically called when you create an object of a class. Are you creating an object on line 11? Commented Apr 5, 2020 at 20:15

1 Answer 1

1

you're calling it implicitly, with wrong / not enough positional parameters here:

OptionMenu(root, default, *ports, command=on_select)

When you invoke a class with parameters, __init__ is called.

help(OptionMenu) shows the initializer signature:

class OptionMenu(Menubutton)
 |  OptionMenu(master, variable, value, *values, **kwargs)

You have to set at least one value. Other values are optional.

usage example:

w = OptionMenu(master, variable, "one", "two", "three")

Here your ports list is empty, so unpacking doesn't yield any argument. Which explains the error. One "fix" would be:

OptionMenu(root, default, *(ports or ["<empty>"]), command=on_select)

If the list is empty, you have only one option (empty). Or check if not empty and raise a user-level message if list is empty

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I tried your code and it works in creating a blank window. However, there are no COM porta available, even though when i connect the arduino. Is there something wrong in my port declaration?
This is a different question.I can't answer that, since I don't know anything about arduino :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.