我無法找到一種方法在我的共享應用程序中刪除一行小部件。我目前正在試驗grid_forget
方法;然而這只是刪除了對象的最後一個實例。我的目標是能夠輸入與我想要刪除的行相關的代碼,然後它將在標籤列表中找到代碼並刪除整行,然後行將自動調整並向上移動以填充任何行白色空間。說實話,我不確定這是否可以用tkinter和python實現,但我只是一個初學者,所以不知道。缺失的代碼看起來是這樣的:如何在tkinter應用程序中刪除特定行標籤小部件
def delete(self, event = None):
if self.code_entry_delete.get() in self.code_list:
self.label1.grid_forget()
self.label2.grid_forget()
…
self.label10.grid_forget()
我遇到的問題是,self.label1…label10
重複了多行,所以忘記self.label1
只會忘記最近的條目。這意味着我需要找到在第x行忘記self.label1
至self.label10
的方法,其中x等於我想要刪除/忘記的行號。
下面我的不完整但工作程序的完整代碼。如果任何人複製並粘貼代碼進行測試,澳大利亞在我們的股票市場上使用3個字母代碼,即。 STO,NAB,CBA。 etc ...
#!/usr/bin/env python3.4
from tkinter import *
import ystockquote
import urllib.request
from urllib.request import urlopen
from bs4 import BeautifulSoup
class Shares(Frame):
def __init__(self, master):
Frame.__init__(self, master)
Frame.__init__(self, master)
self.frame = Frame(master, bg = "#ededed")
self.frame.grid(row = 9, column = 0)
self.canvas = Canvas(self.frame, bg = "#ffffff")
self.data_frame = Frame(self.canvas, bg = "#ffffff")
self.vscrollbar = Scrollbar(self.frame, orient = "vertical", command = self.canvas.yview)
self.canvas.configure(yscrollcommand = self.vscrollbar.set)
self.vscrollbar.grid(row = 9, column = 11, sticky = NS)
self.canvas.config(width = 1210, height = 200)
self.canvas.grid(row = 9, column = 0, columnspan = 11, sticky = E)
self.canvas.create_window((0, 0), window = self.data_frame, anchor = "nw", tags = "self.frame")
self.data_frame.bind("<Configure>", self.onFrameConfig)
self.data_frame.grid_columnconfigure(0, minsize = 352)
self.data_frame.grid_columnconfigure(1, minsize = 96)
self.data_frame.grid_columnconfigure(2, minsize = 96)
self.data_frame.grid_columnconfigure(3, minsize = 96)
self.data_frame.grid_columnconfigure(4, minsize = 96)
self.data_frame.grid_columnconfigure(5, minsize = 96)
self.data_frame.grid_columnconfigure(6, minsize = 96)
self.data_frame.grid_columnconfigure(7, minsize = 96)
self.data_frame.grid_columnconfigure(8, minsize = 96)
self.data_frame.grid_columnconfigure(9, minsize = 96)
master.bind("<Return>", self.company_details)
Label(self.frame, bg = "#ededed", font = "Helvetica 20", text = "Enter ASX Code").grid(row = 0, column = 0, sticky = W, padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", font = "Helvetica 20", text = "Current Price").grid(row = 2, column = 0, sticky = W, padx = 5, pady = 5)
self.entryVar = StringVar()
Entry(self.frame, width = 12, font = "Helvetica 20", textvariable = self.entryVar).grid(row = 0, column = 1, padx = 5, pady = 5)
self.companyVar = StringVar()
Label(self.frame, font = "Helvetica 20", textvariable = self.companyVar).grid(row = 1, column = 0, columnspan = 2, sticky = W, padx = 5, pady = 5)
self.priceVar = DoubleVar()
Label(self.frame, font = "Helvetica 20", textvariable = self.priceVar).grid(row = 2, column = 1, sticky = W, padx = 5, pady = 5)
self.priceVar.set("")
Label(self.frame, bg = "#ededed", font = "Helvetica 20", text = "Current Action").grid(row = 3, column = 0, sticky = W, padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", font = "Helvetica 20", text = "Current Holdings").grid(row = 7, column = 0, pady = 5)
Label(self.frame, bg = "#ededed", width = 10, font = "Helvetica 14 bold", text = "Name").grid(row = 8, column = 0, columnspan = 2, padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", width = 10, font = "Helvetica 14 bold", text = "Code").grid(row = 8, column = 2, padx = 5, pady = 5, sticky = E)
Label(self.frame, bg = "#ededed", width = 10, font = "Helvetica 14 bold", text = "Buy Date").grid(row = 8, column = 3, padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", width = 10, font = "Helvetica 14 bold", text = "Quantity").grid(row = 8, column = 4, padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", width = 10, font = "Helvetica 14 bold", text = "Paid").grid(row = 8, column = 5, padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", width = 10, font = "Helvetica 14 bold", text = "Brokerage").grid(row = 8, column = 6, padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", width = 10, font = "Helvetica 14 bold", text = "Total").grid(row = 8, column = 7, padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", width = 10, font = "Helvetica 14 bold", text = "Current").grid(row = 8, column = 8, padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", width = 10, font = "Helvetica 14 bold", text = "Total").grid(row = 8, column = 9,padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", width = 10, font = "Helvetica 14 bold", text = "Loss/Gain").grid(row = 8, column = 10, padx = 5, pady = 5)
Label(self.frame, bg = "#ededed", text = "Amount").grid(row = 16, column = 10, padx = 5, pady = 5, sticky = E)
Label(self.frame, bg = "#ededed", text = "Amount").grid(row = 16, column = 9, padx = 5, pady = 5, sticky = E)
Label(self.frame, bg = "#ededed", text = "Amount").grid(row = 16, column = 8, padx = 5, pady = 5, sticky = E)
Button(self.frame, width = 11, text = "New Record", command = self.new_record).grid(row = 16, column = 0, padx = 5, pady = 5, sticky = E)
#Button(self.frame, width = 11, text = "Modify Record", command = self.new_record).grid(row = 16, column = 1, padx = 5, pady = 5)
Button(self.frame, width = 11, text = "Delete Record", command = self.delete_record).grid(row = 16, column = 2, padx = 5, pady = 5, sticky = E)
self.rows = 0
self.code_list = []
def validate_code(self):
soup = BeautifulSoup(urllib.request.urlopen("https://au.finance.yahoo.com/q?s=" + (self.code_entry.get() + ".AX")).read(), "html.parser")
element = soup.find("div", ("class", "title"))
name = element.find("h2")
self.name_entry.set(name.text[:-9])
def new_record(self):
top = self.top = Toplevel()
top.bind("<Return>", self.add)
Label(top, text = "Code: ").grid(row = 0, column = 0, sticky = E, padx = 5, pady = 5)
Label(top, text = "Name: ").grid(row = 2, column = 0, sticky = E, padx = 5, pady = 5)
Label(top, text = "Buy Date: ").grid(row = 3, column = 0, sticky = E, padx = 5, pady = 5)
Label(top, text = "Quantity: ").grid(row = 4, column = 0, sticky = E, padx = 5, pady = 5)
Label(top, text = "Paid: ").grid(row = 5, column = 0, sticky = E, padx = 5, pady = 5)
Label(top, text = "Brokerage: ").grid(row = 6, column = 0, sticky = E, padx = 5, pady = 5)
self.name_main = StringVar()
self.label1 = Label(self.data_frame, textvariable = self.name_main)
self.label1.grid(row = self.rows, column = 0, sticky = W)
self.name_main.set("")
self.name_entry = StringVar()
Entry(top, textvariable = self.name_entry).grid(row = 2, column = 1, padx = 5, pady = 5)
self.name_entry.set("")
self.code_main = StringVar()
self.label2 = Label(self.data_frame, textvariable = self.code_main)
self.label2.grid(row = self.rows, column = 1, sticky = EW)
self.code_main.set("")
self.code_entry = StringVar()
Entry(top, textvariable = self.code_entry, validatecommand = self.validate_code).grid(row = 0, column = 1, padx = 5, pady = 5)
self.code_entry.set("")
self.date_main = StringVar()
self.label3 = Label(self.data_frame, textvariable = self.date_main)
self.label3.grid(row = self.rows, column = 2, sticky = EW)
self.date_main.set("")
self.date_entry = StringVar()
Entry(top, textvariable = self.date_entry).grid(row = 3, column = 1, padx = 5, pady = 5)
self.date_entry.set("")
self.quantity_main = IntVar()
self.label4 = Label(self.data_frame, textvariable = self.quantity_main)
self.label4.grid(row = self.rows, column = 3, sticky = E)
self.quantity_main.set("")
self.quantity_entry = IntVar()
Entry(top, textvariable = self.quantity_entry).grid(row = 4, column = 1, padx = 5, pady = 5)
self.quantity_entry.set("")
self.paid_main = DoubleVar()
self.label5 = Label(self.data_frame, textvariable = self.paid_main)
self.label5.grid(row = self.rows, column = 4, sticky = E)
self.paid_main.set("")
self.paid_entry = DoubleVar()
Entry(top, textvariable = self.paid_entry).grid(row = 5, column = 1, padx = 5, pady = 5)
self.paid_entry.set("")
self.brokerage_main = DoubleVar()
self.label6 = Label(self.data_frame, textvariable = self.brokerage_main)
self.label6.grid(row = self.rows, column = 5, sticky = E)
self.brokerage_main.set("")
self.brokerage_entry = DoubleVar()
Entry(top, textvariable = self.brokerage_entry).grid(row = 6, column = 1, padx = 5, pady = 5)
self.brokerage_entry.set(29.95)
self.total_main = DoubleVar()
self.label7 = Label(self.data_frame, textvariable = self.total_main)
self.label7.grid(row = self.rows, column = 6, sticky = E)
self.total_main.set("")
self.current_main = DoubleVar()
self.label8 = Label(self.data_frame, textvariable = self.current_main)
self.label8.grid(row = self.rows, column = 7, sticky = E)
self.current_main.set("")
self.total_two_main = DoubleVar()
self.label9 = Label(self.data_frame, textvariable = self.total_two_main)
self.label9.grid(row = self.rows, column = 8, sticky = E)
self.total_two_main.set("")
self.loss_gain_main = DoubleVar()
self.label10 = Label(self.data_frame, textvariable = self.loss_gain_main)
self.label10.grid(row = self.rows, column = 9, sticky = E)
self.loss_gain_main.set("")
Button(top, text = "Validate code", command = self.validate_code).grid(row = 1, column = 1, padx = 5, pady = 5)
Button(top, text = "Add", command = self.add).grid(row = 7, column = 1, sticky = W, padx = 5, pady = 5)
Button(top, text = "Cancel", command = self.cancel).grid(row = 7, column = 1, sticky = E, padx = 5, pady = 5)
def add(self, event = None):
self.rows += 1
self.code_list.append(self.code_entry.get())
code = str.upper(self.code_entry.get())
self.code_main.set(code)
self.name_main.set(self.name_entry.get())
self.date_main.set(self.date_entry.get())
self.quantity_main.set(self.quantity_entry.get())
paid = self.paid_entry.get()
self.paid_main.set("$%.2f" % paid)
brokerage = self.brokerage_entry.get()
self.brokerage_main.set("$%.2f" % brokerage)
total = self.quantity_entry.get() * self.paid_entry.get() + self.brokerage_entry.get()
self.total_main.set("$%.2f" % total)
self.current_main.set("$" + str(ystockquote.get_price(self.code_entry.get() + ".AX")))
curent_main = float(ystockquote.get_price(self.code_entry.get() + ".AX"))
total_two = curent_main * self.quantity_entry.get()
self.total_two_main.set("$%.2f" % total_two)
rounded = ((total_two/total * 100) - 100)
self.loss_gain_main.set("%.2f" % rounded + "%")
self.top.destroy()
def cancel(self, event = None):
self.top.destroy()
def delete_record(self):
top = self.top = Toplevel()
top.bind("<Return>", self.delete)
Label(top, text = "Please enter code of record to be deleted: ").grid(row = 0, column = 0, sticky = E, padx = 5, pady = 5)
self.code_entry_delete = StringVar()
Entry(top, textvariable = self.code_entry_delete).grid(row = 0, column = 1, padx = 5, pady = 5)
self.code_entry_delete.set("")
Button(top, text = "Delete Record", command = self.delete).grid(row = 1, column = 1, padx = 5, pady = 5)
Button(top, text = "Cancel", command = self.cancel).grid(row = 1, column = 0, padx = 5, pady = 5)
def delete(self, event = None):
if self.code_entry_delete.get() in self.code_list:
self.label1.grid_forget()
self.label2.grid_forget()
self.label3.grid_forget()
self.label4.grid_forget()
self.label5.grid_forget()
self.label6.grid_forget()
self.label7.grid_forget()
self.label8.grid_forget()
self.label9.grid_forget()
self.label10.grid_forget()
self.top.destroy()
def company_details(self, event):
self.priceVar.set("$" + (ystockquote.get_price(self.entryVar.get() + ".AX")))
soup = BeautifulSoup(urllib.request.urlopen("https://au.finance.yahoo.com/q?s=" + (self.entryVar.get() + ".AX")).read(), "html.parser")
element = soup.find("div", ("class", "title"))
name = element.find("h2")
self.companyVar.set(name.text[:-9])
def onFrameConfig(self, event):
self.canvas.configure(scrollregion = self.canvas.bbox("all"))
if __name__ == "__main__":
master = Tk()
master.title("Share Program")
app = Shares(master)
master.geometry("1235x528+20+40")
master.mainloop()
我建議建立大量框架,以保持標籤的,所以你可以刪除整個框架。 –
感謝您的建議 - 我無法真正理解這將如何工作,但我不認爲這是可行的,因爲我已經將框架設置在具有滾動條的畫布內,因此您可以添加儘可能多的行只要你願意,畫布就會展開以迎合它。我看到它的方式,創建大量的幀將限制我創建多少幀的條目。 – SGB
是的,但您可以根據需要創建任意數量的幀。 –