Python Tkinter标签在过去的文本上书写。使用列表,因此无法使用StringVar()
这就是我的问题。。我正在使用tkinter和sockets制作一个messenger应用程序,但是对于我的GUI,我正在使用label小部件来显示传入和传出的消息。我试图做的是让一个标签从一个列表中检索一个项目,就像代码中显示的那个一样,并将其显示在屏幕上。我尝试使用字符串变量,但它无法调用列表中的项。我已经做了很多年了,所以如果有人知道修复方法或者更好的方法,请告诉我Python Tkinter标签在过去的文本上书写。使用列表,因此无法使用StringVar(),python,tkinter,Python,Tkinter,这就是我的问题。。我正在使用tkinter和sockets制作一个messenger应用程序,但是对于我的GUI,我正在使用label小部件来显示传入和传出的消息。我试图做的是让一个标签从一个列表中检索一个项目,就像代码中显示的那个一样,并将其显示在屏幕上。我尝试使用字符串变量,但它无法调用列表中的项。我已经做了很多年了,所以如果有人知道修复方法或者更好的方法,请告诉我 # Easy settings allow_incoming_connections = True # Allows anyo
# 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()
self.updatewig()
def updatewig(self):
self.user_input.set("")
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)
self.textdisp = Label(self, text = window.cache[0]).grid(row=1, column=1, sticky=W)
self.textdisp = Label(self, text = window.cache[1]).grid(row=2, column=1, sticky=W)
self.textdisp = Label(self, text = window.cache[2]).grid(row=3, column=1, sticky=W)
self.textdisp = Label(self, text = window.cache[3]).grid(row=4, column=1, sticky=W)
self.textdisp = Label(self, text = window.cache[4]).grid(row=5, column=1, sticky=W)
self.textdisp = Label(self, text = window.cache[5]).grid(row=6, column=1, sticky=W)
self.textdisp = Label(self, text = window.cache[6]).grid(row=7, column=1, sticky=W)
self.textdisp = Label(self, text = window.cache[7]).grid(row=8, column=1, sticky=W)
self.textdisp = Label(self, text = window.cache[8]).grid(row=9, column=1, sticky=W)
self.textdisp = Label(self, text = window.cache[9]).grid(row=10, column=1, sticky=W)
self.textdisp = Label(self, text = window.cache[10]).grid(row=11, column=1, sticky=W)
self.grid()
def send(self):
#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()
#轻松设置
allow_incoming_connections=True#允许任何拥有您的ip和端口的人与您连接。(“真”或“假”)
con_url=“google.com”
con_ip=“”
con_port=“80”
#添加IP阻止程序。sock.deny(idktheactuaulcode)
#不要搞乱编程。。。它几乎连在一起xD
已连接=错误
从随机输入范围
从tkinter导入ttk
从tkinter进口*
将tkinter作为tk、socket、sys导入,将tkinter.messagebox作为tm、random导入
window=Tk()
窗口几何(“500x260”)
窗口。可调整大小(宽度=假,高度=假)
window.iconbitmap(无)#编码完成后制作图标
窗口标题(“私人信使”)
window.cache=['TkMessage','-Base 64数据加密','-/host或/加入聊天室','-Saving supported','','-/help for help','','','','',请选择别名:']
window.alias=StringVar()
如果con_ip==“”:
con_ip=socket.gethostbyname(con_url)
TK类框架(框架):
定义初始(自我,主):
超级()。\uuuu初始化\uuuuu(主)
window.bind(“,self.send)
sock=socket.socket(socket.AF\u INET,socket.sock\u流)
绑定((socket.gethostbyname(socket.gethostname()),4545))
sock=socket.创建连接((连接ip,连接端口),5)
#变数
self.user_input=StringVar()
self.updatewig()
def updatewig(自我):
self.user\u input.set(“”)
输入框=输入(self,textvariable=self.user\u输入,宽度=65)。网格(行=12,列=1,粘性=W)
输入按钮=ttk.button(self,command=self.send,text=“send”).grid(行=12,列=2,粘性=W)
self.textdisp=Label(self,text=window.cache[0]).grid(行=1,列=1,粘性=W)
self.textdisp=Label(self,text=window.cache[1]).grid(行=2,列=1,粘性=W)
self.textdisp=Label(self,text=window.cache[2]).grid(行=3,列=1,粘性=W)
self.textdisp=Label(self,text=window.cache[3]).grid(行=4,列=1,粘性=W)
self.textdisp=Label(self,text=window.cache[4]).grid(行=5,列=1,粘性=W)
self.textdisp=Label(self,text=window.cache[5]).grid(行=6,列=1,粘性=W)
self.textdisp=Label(self,text=window.cache[6]).grid(行=7,列=1,粘性=W)
self.textdisp=Label(self,text=window.cache[7]).grid(行=8,列=1,粘性=W)
self.textdisp=Label(self,text=window.cache[8]).grid(行=9,列=1,粘性=W)
self.textdisp=Label(self,text=window.cache[9]).grid(行=10,列=1,粘性=W)
self.textdisp=Label(self,text=window.cache[10]).grid(行=11,列=1,粘性=W)
self.grid()
def发送(自我):
#self.textdisp.draw()
尝试:
self.char\u check=str(self.user\u input.get())[0]
除:
通过
打印(自我字符检查)
self.command=self.user\u input.get()
如果self.char\u check==“/”:
window.cache.append(self.user\u input.get())
del window.cache[0]
self.user\u input.set(“”)
self.function()
其他:
尝试:
#发送
window.cache.append(((“Me>”+self.user\u input.get()))
del window.cache[0]
除:
打印(“无法发送消息”)
打印(window.cache)
self.updatewig()
def功能(自我):
#一个长if语句,用于定义所有命令
如果self.command==“/host”:
window.cache.append((“此命令尚不可用。”)
del window.cache[0]
elif self.command==“/join”:
window.cache.append((“输入IP:”)
window.con\u ip=self.user\u input.get()
del window.cache[0]
window.cache.append((“输入端口:”)
window.con_port=self.user_input.get()
del window.cache[0]
window.cache.append((“输入别名:”)
window.alias=self.user\u input.get()
del window.cache[0]
elif self.command==“/changealis”:
window.cache.append((“此命令尚不可用。”)
del window.cache[0]
elif self.command==“/close”:
window.cache.append((“此命令尚不可用。”)
del window.cache[0]
elif self.command==“/ban”:
window.cache.append((“此命令尚不可用。”)
del window.cache[0]
elif self.command==“/encodeon”:
window.cache.append((“此命令尚不可用。”)
del window.cache[0]
elif self.command==“/encodeoff”:
window.cache.append((“此命令尚不可用。”)
del window.cache[0]
elif self.command==“/commands”:
window.cache.append((“此命令尚不可用。”)
del window.cache[0]
elif self.command==“/mysettings”:
window.cache.append((“此命令尚不可用。”)
del window.cache[0]
elif self.command==“/reset”:
window.cache.append((“此命令尚不可用。”)
del window.cache[0]
帧=tkframe(窗口)
window.mainloop()
在调用self.updatewig
时,您实际上是在现有的小部件上创建新的标签
小部件(除了条目
和按钮
小部件),而不仅仅是更改文本。您需要使用confi
# 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()