【发布时间】:2020-06-07 04:33:55
【问题描述】:
这是我需要的 UI。我已经制作了 UI 的左侧。但是,我无法在右侧添加框架。我认为我对所有元素的固有定位是错误的,因此我无法获得框架的右侧位置。在留言板下,我需要一个可以插入文本的框架。有人可以帮我写代码。 我当前的代码是:
from tkinter import *
from tkinter import ttk
import requests
class mainUI:
def __init__(self, root, username, data):
self.root = root
self.root.geometry("900x700")
self.root.title("Auto Login for " + username)
print(data)
# creating categories listbox
Label(self.root, text='Categories', font=('Times 13'), fg="red", padx=20, pady=20).grid(row=0, column=0, sticky=W)
self.categoriesListBox = Listbox(self.root, width=25)
self.categoriesListBox.grid(row=1, column=0, padx=10)
#adding the categories
self.categoriesList = data['Categories']
for item in self.categoriesList:
self.categoriesListBox.insert(END, item['Name'])
# binding double click event on categories
self.categoriesListBox.bind('<Double-1>', self.categorySelect)
# creating websites listbox
Label(self.root, text='Websites', font=('Times 13'), fg="red", padx=20, pady=20).grid(row=19, column=0, sticky=W)
self.wesbitesListBox = Listbox(self.root, width=25)
self.wesbitesListBox.grid(row=20, column=0, padx=10)
#creating the messages frame
Label(self.root, text="Message Board", fg = "red",font=('Times 13'), padx=20, pady=0).grid(row=0, column=1)
self.messageBoardFrame = Frame(self.root, width=200, height=200, background="white", pady=30).grid(row=1, column=1)
# self.messageBoardFrame = LabelFrame(text="Message Board")
Label(self.messageBoardFrame, text="Text").grid(row=1, column=1)
def categorySelect(self, event):
self.wesbitesListBox.delete(0,END)
item = self.categoriesListBox.get('active')
for name in self.categoriesList:
if name['Name'] == item:
websites = name['Websites']
break
print(websites)
for website in websites:
self.wesbitesListBox.insert(END, website)
if __name__ == '__main__':
root = Tk()
root.geometry('585x515+500+200')
application = mainUI(root, "Name", {
'Categories': [{'Name': '31West', 'Websites': ['a','a','a','a']}, {'Name': 'Book Domicile', 'Websites': ['b','b','b','b']},
{'Name': 'Crate', 'Websites': []}, {'Name': 'Electrosonic Inc', 'Websites': []},
{'Name': 'HostGenius', 'Websites': []}, {'Name': 'Insane Audio', 'Websites': []},
{'Name': 'Itsy Bitsy', 'Websites': []}, {'Name': 'Jobs', 'Websites': []},
{'Name': 'Kobe Steakhouse', 'Websites': []}, {'Name': 'MIS Computer Corp.', 'Websites': []},
{'Name': 'Natrinsic', 'Websites': []}, {'Name': 'Ramshyam', 'Websites': []},
{'Name': 'TEST CO', 'Websites': []}, {'Name': 'W-Appliance', 'Websites': []}],
'isLoggedIn': True}
)
root.mainloop()
enter code here
【问题讨论】:
-
你有没有考虑过制作左右框架,然后把你的小部件放进去?在我看来,这样的定位会容易得多。我的确切意思是:
left_frame = Frame(root) left_frame.grid(row=0, column=0, sticky='nswe')然后将类别和网站放在这个框架中,然后创建正确的框架,但在column=1 -
我没有考虑过。我对 Tkinter 还很陌生,所以我不太确定应该如何做。如果您认为代码不需要时间,您能否编辑我的代码以反映所需的 UI?问,因为我认为所需的大部分元素都是由我的代码提供的。只是不知道如何使用框架来格式化它。
标签: python tkinter tkinter-canvas tkinter-layout tkinter-scale