bind.()函数只发送最后一个参数

2024-04-25 00:43:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python使用tkinter开发一个聊天程序,我想让用户能够单击一个名字并在一个新的tkinter窗口中打开一个聊天,但是当我单击这些名字时,只会传递姓氏。你知道吗

我最初使用while循环遍历数据库中的不同用户。你知道吗

希望这个错误能被修复,我切换到for循环,但什么也没变。你知道吗

这是我当前的代码:

def openchat(event):
root= Tk()
root.title(contactname)
print("everything binds to this")
print("contactname: %s" % contactname)


def contacts():

c.execute("SELECT contact_id FROM contacts WHERE user_id = ?", (session_id[0], ))
row = c.fetchall();

print("prints contact id: %s" % row, "\n")
rowlength = len(row)
x = 0
print(row[x][0])

#while x < rowlength:
for i in row:
    c.execute("SELECT * FROM users WHERE user_id = ?", (row[x][0], ))
    row2 = c.fetchone();
    print("('contact_id', 'contactname', 'password', 'chat_id')")
    print(row2)
    contactname[0] = row2[1]
    print("contactname: %s" % contactname, "x: %s" % x, "\n")
    contactframe = Frame(leftframe, bg="#00A098", highlightthickness=2.5, highlightbackground="Black")
    contactframe.pack(side = TOP, fill=X)
    contactframe.bind("<Button-1>", lambda event: openchat(contactname))
    n = Label(contactframe, text=contactname, fg="Black", bg='#00A098')
    n.config(font=("Ariel", 20))
    n.bind("<Button-1>", lambda event: openchat(contactname))
    n.pack()
    x+=1

这是我运行程序时得到的终端输出:

contact function: 

prints contact id: [('2',), ('3',), ('4',)] 

2
('contact_id', 'contactname', 'password', 'chat_id')
('2', 'brian', 'brian', '2')
contactname: ['brian'] x: 0 

('contact_id', 'contactname', 'password', 'chat_id')
('3', 'abdi', 'abdi', '3')
contactname: ['abdi'] x: 1 

('contact_id', 'contactname', 'password', 'chat_id')
('4', 'john', 'john', '4')
contactname: ['john'] x: 2 

以上所有的工作完全如预期,但这是我搞砸了。一切都和最后一次接触有关,约翰

contactname: ['john']
contactname: ['john']
contactname: ['john']

但它应该显示如下内容:

contactname: ['brian']
contactname: ['abdi']
contactname: ['john']

我目前的理论是,绑定函数只在我点击按钮时运行,因此只输出最后一个联系人的名字,但我可能错了。你知道吗

如果有人能告诉我哪里做错了,或者给我指出了正确的方向,我将不胜感激。你知道吗

提前谢谢

===============

我找到了一个适合我的解决方案,这是我的最终代码,以防其他人有类似的问题:

def openchat(i):
  c.execute("SELECT * FROM users WHERE user_id = ?", (i, ))
  row = c.fetchall();
  contactname = row[0][1]
  root= Tk()
  root.title(contactname)
  print("contactname: %s" % contactname)


def contacts(self):

  c.execute("SELECT contact_id FROM contacts WHERE user_id = ?", (session_id[0], ))
  row = c.fetchall();

  print("prints contact id: %s" % row, "\n")
  rowlength = len(row)
  minrow = min(row)
  maxrow = max(row)
  minrow2 = minrow[0]
  maxrow2 = maxrow[0]
  minrow2 = int(minrow2)
  maxrow2 = int(maxrow2)
  x = 0
  print(row[x][0])

  for i in range(minrow2,maxrow2+1):

      self.contact_id = i
      c.execute("SELECT * FROM users WHERE user_id = ?", (row[x][0], ))
      row2 = c.fetchone();
      print(row2)
      contactname[0] = row2[1]
      print("contactname: %s" % contactname, "x: %s" % x, "\n")
      contactframe = Frame(leftframe, bg="#00A098", highlightthickness=2.5, highlightbackground="Black")
      contactframe.pack(side = TOP, fill=X)
      n = Button(contactframe, text=contactname, fg="Black", bg='#00A098', command = lambda i=i: openchat(i))
      #n.config(font=("Ariel", 20))
      n.pack()
      x+=1

Tags: fromidexecutedefcontactwherejohnselect
1条回答
网友
1楼 · 发布于 2024-04-25 00:43:20

Comment: when I use 'text' I get this error _tkinter.TclError: unknown option "-text"

以下是我的作品:

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        n = tk.Label(self, text='test', fg="Black", bg='#00A098')
        n.config(font=("Ariel", 20))
        n.bind("<Button-1>", self.openchat)
        n.grid(row=0, column=0)

    def openchat(self, event):
        print("event from {}".format(type(event.widget))
        # >>> event from <class 'tkinter.Label'>
        print("event from {}".format(event.widget['text']))
        # >>> event from test

if __name__ == "__main__":
    App().mainloop()

用Python测试:3.5-'TclVersion':8.6'TkVersion':8.6

相关问题 更多 >