我对python来说还很陌生,我试图用9个输入框设置TKinter接口。我使用下面的第一行代码来跟踪每个小部件的值,但是当我尝试使用下面的第二行来清理代码时,我发现所有三个嵌套数组都绑定到相同的3个小部件。我觉得这里缺少明显的东西。
data = [[StringVar(), StringVar(), StringVar()],
[StringVar(), StringVar(), StringVar()],
[StringVar(), StringVar(), StringVar()]]
data = [[StringVar() for i in range(3)]for x in range(3)]
编辑: 我要发布整个代码,
from tkinter import *
from tkinter import ttk, StringVar
import asyncio
import websockets
ip = "10.30.128.92"
port = 5678
root = Tk()
status = StringVar()
async def send_data(*args):
buffer = [[0]*3]*3
try:
for i in range(3):
for j in range(3):
val = float(data[i][j].get())
if 0 < val < 255:
buffer[i][j] = val
elif val >= 255:
buffer[i][j] = 255
else:
buffer[i][j] = 0
print(buffer)
except ValueError:
pass
status.set("Connecting...")
root.update()
await asyncio.sleep(1)
try:
async with websockets.client.connect("ws://{}:{}".format(ip, port)) as websocket:
status.set("Connected")
root.update()
await asyncio.sleep(1)
status.set("Transmitting Values")
root.update()
await asyncio.sleep(1.3)
print("Connection at ws://{}:{} established".format(ip, port))
print("Awaiting Server reply code")
msg = await websocket.recv()
print(msg)
print("Transmitting RGB values")
await websocket.send(str(buffer))
status.set("Data Transmitted ✓")
root.update()
await asyncio.sleep(1)
print("Closing connection at ws://{}:{}".format(ip, port))
websocket.close()
except ConnectionRefusedError:
status.set("Unable to Connect")
root.update()
await asyncio.sleep(1)
print("Unable to connect to server at w://{}:{}".format(ip, port))
def run(*args):
asyncio.run(send_data(args))
root.title("LED Manager")
root.iconbitmap('exbICO.ico')
mainframe = ttk.Frame(root, padding=".3i")
mainframe.grid(column=0, row=0)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
#Holds the values of each entry box
#data = [[StringVar(), StringVar(), StringVar()],
# [StringVar(), StringVar(), StringVar()],
# [StringVar(), StringVar(), StringVar()]]
data =[[StringVar() for i in range(3)] for x in range(3)]
print(data)
#Holds references to the entry box widgets
data_entry = [[0]*3]*3
#Creates ttk Entry widgets that feed into values of data[][] and the places them into the frame
for i in range(3):
for j in range(3):
data_entry[i][j] = ttk.Entry(mainframe, width=7, textvariable=data[i][j])
data_entry[i][j].grid(column=i+1, row=j+2)
data_entry[i][j].focus()
#Creates the labels on all entry boxes and buttons
ttk.Label(mainframe, textvariable=status).grid(column=4, row=2)
ttk.Label(mainframe, text="Red 0-255").grid(column=0, row=2)
ttk.Label(mainframe, text="Green 0-255").grid(column=0, row=3)
ttk.Label(mainframe, text="Blue 0-255").grid(column=0, row=4)
ttk.Label(mainframe, text="LED #1").grid(column=1, row=1)
ttk.Label(mainframe, text="LED #2").grid(column=2, row=1)
ttk.Label(mainframe, text="LED #3").grid(column=3, row=1)
ttk.Button(mainframe, text="Send", command=run).grid(column=4, row=3)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.bind('<Return>', run)
while True:
try:
root.update_idletasks()
root.update()
except TclError:
sys.exit(0)
与列表注释一起运行时,与其他注释未注释的行相比,对我来说会产生不同的行为
答案 0 :(得分:3)
他们做同样的事情:
collection.find("{},{imageData:0,thumbnail:0}")
产量:
from tkinter import * win = Tk() data = [[StringVar(), StringVar(), StringVar()], [StringVar(), StringVar(), StringVar()], [StringVar(), StringVar(), StringVar()]] print(data) data = [[StringVar() for i in range(3)]for x in range(3)] print(data)
和
[[<tkinter.StringVar object at 0x0000024E4EC16470>, <tkinter.StringVar object at 0x0000024E4EC162B0>, <tkinter.StringVar object at 0x0000024E4EE7E400>], [<tkinter.StringVar object at 0x0000024E4EF8B3C8>, <tkinter.StringVar object at 0x0000024E4EF8B438>, <tkinter.StringVar object at 0x0000024E4EF8B4A8>], [<tkinter.StringVar object at 0x0000024E4EF8B518>, <tkinter.StringVar object at 0x0000024E4EF8B588>, <tkinter.StringVar object at 0x0000024E4EF8B5F8>]]
如果您要问为什么[[<tkinter.StringVar object at 0x0000024E4EF8B6D8>, <tkinter.StringVar object at 0x0000024E4EF8B668>, <tkinter.StringVar object at 0x0000024E4EF8B748>],
[<tkinter.StringVar object at 0x0000024E4EF8B780>, <tkinter.StringVar object at 0x0000024E4EF8B7B8>, <tkinter.StringVar object at 0x0000024E4EF8B7F0>],
[<tkinter.StringVar object at 0x0000024E4EF8B828>, <tkinter.StringVar object at 0x0000024E4EF8B860>, <tkinter.StringVar object at 0x0000024E4EF8B898>]]
!= 0x0000024E4EC16470
(第一个实例)是因为该数字告诉您对象在内存中的位置。都是0x0000024E4EF8B6D8
对象,但它们位于不同的位置。例如,我想象您的邻居也是StringVar
对象,但是由于他们是您的邻居,因此它们具有不同的地址!