Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python _uinit()错误,尽管我没有使用init()_Python_Python 3.x_Tkinter - Fatal编程技术网

Python _uinit()错误,尽管我没有使用init()

Python _uinit()错误,尽管我没有使用init(),python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我是Python新手。 我尝试使用它来创建GUI,以便控制我的arduino 因为我不完全理解python,更不用说Tkinter或pySerial了,所以我在网上找到了一段代码 该代码段应该创建一个GUI,并且还有一个选择COM端口的选项,用于与arduino通信 代码如下: import serial.tools.list_ports from tkinter import * def on_select(selection): # open the port and comman

我是Python新手。 我尝试使用它来创建GUI,以便控制我的arduino

因为我不完全理解python,更不用说Tkinter或pySerial了,所以我在网上找到了一段代码

该代码段应该创建一个GUI,并且还有一个选择COM端口的选项,用于与arduino通信

代码如下:

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()
我运行了这个(我有Python 3.8.2),这是我得到的错误:

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'
回溯(最近一次呼叫最后一次):
文件“code.py”,第11行,在
OptionMenu(根目录,默认,*端口,命令=on_select).pack()
TypeError:\uuuu init\uuuu()缺少1个必需的位置参数:“值”

但是,我没有一个init()来传递参数。

您隐式调用它,这里的位置参数错误/不够:

OptionMenu(root, default, *ports, command=on_select)
当您使用参数调用类时,将调用
\uuuuu init\uuuu

帮助(选项菜单)
显示初始值设定项签名:

class OptionMenu(Menubutton)
 |  OptionMenu(master, variable, value, *values, **kwargs)
您必须至少设置一个值。其他值是可选的

用法示例:

w = OptionMenu(master, variable, "one", "two", "three")
这里您的
端口列表是空的,因此解包不会产生任何参数。这就解释了这个错误。一个“解决方案”是:

OptionMenu(root, default, *(ports or ["<empty>"]), command=on_select)
OptionMenu(根目录,默认值,*(端口或[“”]),command=on\u选择)

如果列表为空,则只有一个选项(空)。或者检查是否不为空,如果列表为空,则引发一条用户级消息

您正在使用错误/位置参数隐式调用它。“初始化”是类的初始化函数。当您创建类的对象时,会自动调用它。您正在第11行创建对象吗?谢谢!我尝试了你的代码,它可以创建一个空白窗口。但是,即使我连接arduino,也没有可用的COM端口。我的港口申报有什么问题吗?这是另一个问题。我不能回答这个问题,因为我对arduino一无所知:)