提问人:Alban Mcguinness 提问时间:11/5/2023 更新时间:11/5/2023 访问量:29
如何使用 Treeview 修复 Tkinter 代码。我正在尝试让一个表来显示表数据,同时能够切换帧
How to fix Tkinter code using Treeview. i am trying to get a table to display table data while being able to switch frames
问:
我是 tkinter 的新手,需要一个表格来显示我的值。我找到了一些有效的表代码,但出现错误。我不能说更多,因为我不明白
这是代码。导致错误的表格内容是从第 127 行到第 183 行。行号可能已关闭,因为我从代码中删除了空格以发布它。 错误:
对不起,如果我发布了太多代码,但上次我被卡住了,很多人因为没有发布所有代码而生我的气。主要部分在bottem。一开始,我定义了工作正常的更改帧,然后我将数据存储在文本文件中,而 bottem 是出现错误的第二个帧。我不确定某些代码是如何工作的,因为我使用示例来创建它。该表应该可以工作,因为我在单独的 python 文件中对其进行了测试。我还测试了第一帧,它工作正常。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\HOME\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "D:\Downloads\protosaurus rex.py", line 91, in <lambda>
Button1= Button(frame1, text="Load Screen", command=lambda:changeFrames(root, "manager"))
File "D:\Downloads\protosaurus rex.py", line 17, in changeFrames
root = ManagerWindowMaker()
File "D:\Downloads\protosaurus rex.py", line 179, in ManagerWindowMaker
tree.grid(row=0, column=0)
File "C:\Users\HOME\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2522, in grid_configure
self.tk.call(
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
import tkinter
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
def changeFrames(theframe, frameName):
theframe.destroy()
if frameName == "staff":
root = Windowmaker()
root.mainloop()
elif frameName == "manager":
root = ManagerWindowMaker()
root.mainloop()
def Windowmaker():
global DateVar,SupnameVar,BuynameVar,CostVar,PriceVar,InvVar,WeightVar,SpecialVar,ContractVar
#Create a rootdow/form
root = Tk()
root.title("PROTOTYPE")
frame1=Frame(root)
frame1.pack()
Label(frame1, font=("Arial 12"), text="ADD LOAD").grid(row=0, column=0)
Label(frame1, text="Date needed").grid(row=1, column=0, sticky=W)
DateVar=StringVar()
Date= Entry(frame1, textvariable=DateVar)
Date.grid(row=1,column=1,sticky=W)
Label(frame1, text="Supplier Name").grid(row=2, column=0, sticky=W)
SupnameVar=StringVar()
Supname= Entry(frame1, textvariable=SupnameVar)
Supname.grid(row=2,column=1,sticky=W)
Label(frame1, text="Buyer Name").grid(row=3, column=0, sticky=W)
BuynameVar=StringVar()
Buyname= Entry(frame1, textvariable=BuynameVar)
Buyname.grid(row=3,column=1,sticky=W)
Label(frame1, text="Cost").grid(row=4, column=0, sticky=W)
CostVar=StringVar()
Cost= Entry(frame1, textvariable=CostVar)
Cost.grid(row=4,column=1,sticky=W)
Label(frame1, text="Price").grid(row=5, column=0, sticky=W)
PriceVar=StringVar()
Price= Entry(frame1, textvariable=PriceVar)
Price.grid(row=5,column=1,sticky=W)
Label(frame1, text="Inventory").grid(row=1, column=3, sticky=W)
InvVar=StringVar()
Inv= Entry(frame1, textvariable=InvVar)
Inv.grid(row=1,column=4,sticky=W)
Label(frame1, text="Weight").grid(row=2, column=3, sticky=W)
WeightVar=StringVar()
Weight= Entry(frame1, textvariable=WeightVar)
Weight.grid(row=2,column=4,sticky=W)
Label(frame1, text="Special").grid(row=3, column=3, sticky=W)
SpecialVar=StringVar()
Special= Entry(frame1, textvariable=SpecialVar)
Special.grid(row=3,column=4,sticky=W)
Label(frame1, text="Contract").grid(row=4, column=3, sticky=W)
ContractVar=StringVar()
Contract= Entry(frame1, textvariable=ContractVar)
Contract.grid(row=4,column=4,sticky=W)
frame2 = Frame(root)
frame2.pack()
Button1= Button(frame1, text="Load Screen", command=lambda:changeFrames(root, "manager"))
Button2= Button(frame1, text="Save load", command=LoadSave)
Button1.grid(row=6,column=4,sticky=W)
Button2.grid(row=5,column=4,sticky=W)
return root
def LoadSave():
DateSave = DateVar.get()
SupnameSave =SupnameVar.get()
BuynameSave = BuynameVar.get()
CostSave = CostVar.get()
PriceSave = PriceVar.get()
InvSave = InvVar.get()
WeightSave = WeightVar.get()
SpecialSave = SpecialVar.get()
ContractSave = ContractVar.get()
Datelength = " "*4
Supnamelength = " "*(20-len(SupnameVar.get()))
Buynamelength = " "*(20-len(BuynameVar.get()))
Costlength = " "*(10-len(CostVar.get()))
Pricelength = " "*(10-len(PriceVar.get()))
Invlength = " "*(20-len(InvVar.get()))
Weightlength = " "*(10-len(WeightVar.get()))
Speciallength = " "*(10-len(SpecialVar.get()))
Contractlength = " "*(10-len(ContractVar.get()))
print((DateSave+Datelength+",")+(SupnameSave+Supnamelength+",")+(BuynameSave+Buynamelength+",")+(CostSave+Costlength+",")+(PriceSave+Pricelength+",")+(InvSave+Invlength+",")+(WeightSave+Weightlength+",")+(SpecialSave+Speciallength+",")+(ContractSave+Contractlength)+ "\n")
fileObject = open("Albans_loads.txt","a")
fileObject.write((DateVar.get()+Datelength+",")+(SupnameSave+Supnamelength+",")+(BuynameSave+Buynamelength+",")+(CostSave+Costlength+",")+(PriceSave+Pricelength+",")+(InvSave+Invlength+",")+(WeightSave+Weightlength+",")+(SpecialSave+Speciallength+",")+(ContractSave+Contractlength)+ "\n")
fileObject.close()
def ManagerWindowMaker():
global StaffIDVar, PostcodeVar
#Create a rootdow/form
root = Tk()
root.title('Treeview demo')
root.geometry('1000x1000')
frame1=Frame(root)
frame1.pack()
Label(frame1, font=("Arial 12"), text="MAIN SCREEN").grid(row=0, column=0)
z=0
for line in open("Albans_loads.txt", "r").readlines():
z += 1
table = [None]*z
p = 0
for line in open("Albans_loads.txt", "r").readlines():
row = [None]*9
y=0
p+=1
x=0
for w in range(0,9):
x += 1
if y < 9:
y += 1
lines = line.split(",")
linetemp = lines[y-1]
linetemp = linetemp.replace(" ", "")
linetemp = linetemp.replace("\n", "")
row[x-1] = linetemp
table[p-1] = row
col = ("Date", "Supname", "Buyname", "Cost", "Price", "Inv", "Weight", "Special", "Contract")
tree = ttk.Treeview(root, columns=col, show="headings")
tree.heading("Date", text = "Date")
tree.heading("Supname", text = "Supplier Name")
tree.heading("Buyname", text = "Buyer Name")
tree.heading("Cost", text = "Cost(£)")
tree.heading("Price", text = "Price(£)")
tree.heading("Inv", text = "Inventory")
tree.heading("Weight", text = "Weight(kg)")
tree.heading("Special", text = "Special")
tree.heading("Contract", text = "On a Contract?")
for load in table:
tree.insert("", tkinter.END, values=load)
tree.grid(row=0, column=0)
scrollbar = ttk.Scrollbar(root, orient=tkinter.VERTICAL, command=tree.yview)
tree.configure(yscroll=scrollbar.set)
scrollbar.grid(row=0, column=1)
frame2 = Frame(root)
frame2.pack()
Button1= Button(frame2, text="Main Screen", command=lambda:changeFrames(root, "staff"))
Button1.pack(side=LEFT)
return root
#this is the main program!
root = Windowmaker()
root.mainloop()
答: 暂无答案
评论
ManagerWindowMaker()
frame1
tree
root
pack()
frame1
grid()
tree