Reputation: 2201
I'm in the process of writing a GUI for my program (see here for the CLI version) and one of the tasks it does is download an information file from dropbox and displays it to the user. The information it downloads is displayed in a label, and then text input from the user, and output from the program, is displayed in a second label below this. Because I can change the information that it downloads, the width of the top label is variable, and I would like the bottom label to change to be the same width as the top label.
I've used the following codes to try and achieve the result, but nothing seems to work, as the bottom label ends up being either too long or too short:
conv.configure(width=inf.winfo_width())
returns 950
conv.configure(width=inf.cget('width'))
returns 0
conv.configure(width=inf['width'])
returns 0
conv.configure(width=len(max(open('information.txt'), key=len)))
returns 178 (the length of the longest line)
Here is the GUI layout that I have written:
from tkinter import *
import urllib
root=Tk()
root.title("ARTIST AI")
inf=Label(root, relief="raised", text="INFORMATION", anchor=N, justify=LEFT)
inf.grid(row=0, column=0, columnspan=2)
conv=Label(root, height=10, relief="raised", anchor=N, justify=LEFT)
conv.grid(row=1, column=0, columnspan=2)
ent=Text(root, width=40, height=2)
ent.grid(row=2, column=0)
sub=Button(root, text="Submit", width=10, height=2, relief="raised", command=getinput)
sub.grid(row=2, column=1)
try: #Downloads the inforrmation from the Info.txt file in my dropbox account to be displayed to the user.
info=urllib.request.urlopen("https://www.dropbox.com/s/h6v538utdp8xrmg/Info.txt?dl=1").read().decode('utf-8')
f=open("information.txt", "w")
f.write(info)
f.close()
except urllib.error.URLError:
try:
info=open("information.txt", "r").read()
info=info.replace("\n\n", "\n")
info="No internet connection available: using last available information"+"\n\n"+info
except FileNotFoundError: info="No information available. Will download information on next valid connection."
inf.configure(text=info)
conv.configure(width=length_of_inf_label)
mainloop()
If anyone knows how to do this, any help is greatly appreciated.
Thanks in advance.
Upvotes: 0
Views: 1884
Reputation: 41
Don't know if you need this anymore, but this was how I did it. It's a little crude but it works:
label1 = Label(root, text="Start")label1.pack({"side": "top"})
label2 = Label(root, text="End")
label2.config(width=len(label1.cget('text'))-1)
label2.pack({"side": "top"})
Upvotes: 1