所以这是我的问题..我正在使用tkinter和套接字创建一个messenger应用程序但是对于我的GUI,我使用标签小部件来显示传入和传出消息。我要做的是让标签从列表中检索项目,如显示的代码中的项目,并将其显示在屏幕上。我尝试使用字符串变量,但它无法从列表中调用项目。我一直在这看似年龄,所以如果有人知道修复或更好的方法,请告诉我。
col-xs-offset-6 col-xs-1"
答案 0 :(得分:1)
实际上,只要您调用[Content_Types].xml
而不是仅仅更改文本,就可以在现有的小部件上创建新的Label
小部件(除了Entry
和Button
小部件)。您需要使用self.updatewig
方法更改标签上的文字。
为此,首先需要将标签创建与展示位置(config
)分开,因为网格返回grid
而您无法在None
上使用config
类型。您还需要为小部件指定不同/单独的名称,以便稍后调用它们。
我将小部件创建移动到None
方法,以便__init__
只会更改标签文字。
以下是代码,我希望它按预期的方式工作:
updatewig
注意强>
# Easy settings
allow_incoming_connections = True # Allows anyone with your ip and port to connect to you. ('True' or 'False')
con_url = "google.com"
con_ip = ""
con_port = "80"
#Add a IP blocker. sock.deny(idktheactuaulcode)
#Dont mess with the programming... it is barely held together xD
connected = False
from random import randrange
from tkinter import ttk
from tkinter import *
import tkinter as tk, socket, sys, tkinter.messagebox as tm, random
window = Tk()
window.geometry("500x260")
window.resizable(width=FALSE, height=FALSE)
window.iconbitmap(None)#make icon later after coding is done
window.title("Private Messenger")
window.cache = ['TkMessage','- Base 64 data encryption','- /host or /join a chat','- Saving supported','','','- /help for help','','','','Please select an alias:']
window.alias = StringVar()
if con_ip == "":
con_ip = socket.gethostbyname(con_url)
class tkframe(Frame):
def __init__(self, master):
super().__init__(master)
window.bind("<Return>", self.send)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((socket.gethostbyname(socket.gethostname()), 4545))
sock = socket.create_connection((con_ip, con_port),5)
#Variables
self.user_input = StringVar()
## Create widgets (input, button, labels) only once
input_box = Entry(self, textvariable = self.user_input, width=65).grid(row=12, column=1, sticky=W)
input_button = ttk.Button(self, command = self.send, text="Send").grid(row=12, column=2, sticky=W)
## Seperate label creation from grid (because grid will return 'None')
## Give labels different names
self.textdisp00 = Label(self, text = "")
self.textdisp01 = Label(self, text = "")
self.textdisp02 = Label(self, text = "")
self.textdisp03 = Label(self, text = "")
self.textdisp04 = Label(self, text = "")
self.textdisp05 = Label(self, text = "")
self.textdisp06 = Label(self, text = "")
self.textdisp07 = Label(self, text = "")
self.textdisp08 = Label(self, text = "")
self.textdisp09 = Label(self, text = "")
self.textdisp10 = Label(self, text = "")
## Grid the labels
self.textdisp00.grid(row=1, column=1, sticky=W)
self.textdisp01.grid(row=2, column=1, sticky=W)
self.textdisp02.grid(row=3, column=1, sticky=W)
self.textdisp03.grid(row=4, column=1, sticky=W)
self.textdisp04.grid(row=5, column=1, sticky=W)
self.textdisp05.grid(row=6, column=1, sticky=W)
self.textdisp06.grid(row=7, column=1, sticky=W)
self.textdisp07.grid(row=8, column=1, sticky=W)
self.textdisp08.grid(row=9, column=1, sticky=W)
self.textdisp09.grid(row=10, column=1, sticky=W)
self.textdisp10.grid(row=11, column=1, sticky=W)
self.updatewig()
def updatewig(self):
## Change content of the labels using 'config'
self.user_input.set("")
self.textdisp00.config(text = window.cache[0])
self.textdisp01.config(text = window.cache[1])
self.textdisp02.config(text = window.cache[2])
self.textdisp03.config(text = window.cache[3])
self.textdisp04.config(text = window.cache[4])
self.textdisp05.config(text = window.cache[5])
self.textdisp06.config(text = window.cache[6])
self.textdisp07.config(text = window.cache[7])
self.textdisp08.config(text = window.cache[8])
self.textdisp09.config(text = window.cache[9])
self.textdisp10.config(text = window.cache[10])
self.grid()
def send(self, *args): ## Pressing the return key throws an error, so added '*args' to make it work
#self.textdisp.withdraw()
try:
self.char_check = str(self.user_input.get())[0]
except:
pass
print(self.char_check)
self.command = self.user_input.get()
if self.char_check == "/":
window.cache.append(self.user_input.get())
del window.cache[0]
self.user_input.set("")
self.function()
else:
try:
#send
window.cache.append(("Me> "+self.user_input.get()))
del window.cache[0]
except:
print("Message could not be sent")
print(window.cache)
self.updatewig()
def function(self):
# One long if statement to define all the commands
if self.command == "/host":
window.cache.append(("This command is not yet available."))
del window.cache[0]
elif self.command == "/join":
window.cache.append(("Enter IP:"))
window.con_ip = self.user_input.get()
del window.cache[0]
window.cache.append(("Enter Port:"))
window.con_port = self.user_input.get()
del window.cache[0]
window.cache.append(("Enter alias:"))
window.alias = self.user_input.get()
del window.cache[0]
elif self.command == "/changealias":
window.cache.append(("This command is not yet available."))
del window.cache[0]
elif self.command == "/close":
window.cache.append(("This command is not yet available."))
del window.cache[0]
elif self.command == "/ban":
window.cache.append(("This command is not yet available."))
del window.cache[0]
elif self.command == "/encodeon":
window.cache.append(("This command is not yet available."))
del window.cache[0]
elif self.command == "/encodeoff":
window.cache.append(("This command is not yet available."))
del window.cache[0]
elif self.command == "/commands":
window.cache.append(("This command is not yet available."))
del window.cache[0]
elif self.command == "/mysettings":
window.cache.append(("This command is not yet available."))
del window.cache[0]
elif self.command == "/reset":
window.cache.append(("This command is not yet available."))
del window.cache[0]
FRAME = tkframe(window)
window.mainloop()
方法在按下send
键时抛出了有关额外参数的错误,因此我添加了Return
作为参数。现在它应该适用于*args
密钥和&#39;发送&#39;按钮。Return
和Text
状态的单个Normal
窗口小部件代替Disable
可能更容易(也更好?)。我没有在答案中使用它,因为我不知道你使用标签的原因。