如何在 tkinter 应用程序中删除特定行的标签小部件
How to delete a specific row of label widgets in tkinter app
我找不到删除我的分享应用中的一行小部件的方法。我目前正在试验 grid_forget
方法;然而,这只是删除了对象的最后一个实例。我的目标是能够输入与我要删除的行相关的代码,然后它将在标签列表中找到代码并删除整行,之后行将自动调整并向上移动以填充任何白色 space。老实说,我不确定这是否可以通过 tkinter 和 python 实现,但我只是一个初学者,所以真的不知道。删除代码如下所示:
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 等于我想要 remove/forget.
的行号
下面是我不完整但可以运行的程序的完整代码。如果有人复制并粘贴代码进行测试,澳大利亚对我们股票市场上的公司使用 3 个字母代码,即。 STO、NAB、CBA。等等……
#!/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()
下面是一些可能对您有所帮助的代码。要创建新的框架调用 add_frame()
,然后删除框架调用 framelist[whatevernumbertheframeis].destroy()
。
要在框架中创建小部件,请转到 Label(framelist[framenumber], text="etc.")
,要在最后创建的框架中创建小部件,请执行 Label(framelist[-1], text="etc.")
。希望这对您有所帮助!
from Tkinter import *
framelist=[]
frames=1
def add_frame():
global frames
framelist.append(Frame(root))
framelist[-1].grid()
frames+=1
root=Tk()
for i in range(20):
add_frame()
Label(framelist[i], text="Frame %i" % (i)).grid()
framelist[6].destroy()
root.mainloop()
现在因为我没有你的模块,所以我不能完美地完成它,但这里有一个你如何完成它的一部分的例子。
P.S代码中还有多个错误...
from Tkinter import *
import ystockquote
import urllib.request
from urllib.request import urlopen
from bs4 import BeautifulSoup
"""
I am adding comments so that if
you need help from Whosebug
on this again they will know whats
what because it took me a looooong time
"""
class Shares(Frame):
# Main page to display gain/loss on different shares
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 = {}
# Function to validate Australian company codes
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])
# Adding a new company
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.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.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.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.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.paid_entry = DoubleVar()
Entry(top, textvariable = self.paid_entry).grid(row = 5, column = 1, padx = 5, pady = 5)
self.paid_entry.set("")
self.frames=[]
self.column=0
self.brokerage_main = DoubleVar()
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.current_main = DoubleVar()
self.total_two_main = DoubleVar()
self.loss_gain_main = DoubleVar()
self.frames.append(Frame(self.data_frame))
self.frames[-1].grid()
Label(self.frames[-1], textvariable=self.name_main).grid(row=self.rows, column=1)
Label(self.frames[-1], textvariable=self.code_main).grid(row=self.rows, column=2)
Label(self.frames[-1], textvariable=self.date_main).grid(row=self.rows, column=3)
Label(self.frames[-1], textvariable=self.quantity_main).grid(row=self.rows, column=4)
Label(self.frames[-1], textvariable=self.paid_main).grid(row=self.rows, column=1)
Label(self.frames[-1], textvariable=self.brokerage_main).grid(row=self.rows, column=6)
Label(self.frames[-1], textvariable=self.total_main).grid(row=self.rows, column=7)
Label(self.frames[-1], textvariable=self.current_main).grid(row=self.rows, column=8)
Label(self.frames[-1], textvariable=self.total_two_main).grid(row=self.rows, column=9)
Label(self.frames[-1], textvariable=self.loss_gain_main).grid(row=self.rows, column=10)
self.rows+=1
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[self.code_entry.get()]=self.frames[-1]
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 = self.current_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.code_list[self.code_entry_delete.get()].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()
我找不到删除我的分享应用中的一行小部件的方法。我目前正在试验 grid_forget
方法;然而,这只是删除了对象的最后一个实例。我的目标是能够输入与我要删除的行相关的代码,然后它将在标签列表中找到代码并删除整行,之后行将自动调整并向上移动以填充任何白色 space。老实说,我不确定这是否可以通过 tkinter 和 python 实现,但我只是一个初学者,所以真的不知道。删除代码如下所示:
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 等于我想要 remove/forget.
下面是我不完整但可以运行的程序的完整代码。如果有人复制并粘贴代码进行测试,澳大利亚对我们股票市场上的公司使用 3 个字母代码,即。 STO、NAB、CBA。等等……
#!/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()
下面是一些可能对您有所帮助的代码。要创建新的框架调用 add_frame()
,然后删除框架调用 framelist[whatevernumbertheframeis].destroy()
。
要在框架中创建小部件,请转到 Label(framelist[framenumber], text="etc.")
,要在最后创建的框架中创建小部件,请执行 Label(framelist[-1], text="etc.")
。希望这对您有所帮助!
from Tkinter import *
framelist=[]
frames=1
def add_frame():
global frames
framelist.append(Frame(root))
framelist[-1].grid()
frames+=1
root=Tk()
for i in range(20):
add_frame()
Label(framelist[i], text="Frame %i" % (i)).grid()
framelist[6].destroy()
root.mainloop()
现在因为我没有你的模块,所以我不能完美地完成它,但这里有一个你如何完成它的一部分的例子。
P.S代码中还有多个错误...
from Tkinter import *
import ystockquote
import urllib.request
from urllib.request import urlopen
from bs4 import BeautifulSoup
"""
I am adding comments so that if
you need help from Whosebug
on this again they will know whats
what because it took me a looooong time
"""
class Shares(Frame):
# Main page to display gain/loss on different shares
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 = {}
# Function to validate Australian company codes
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])
# Adding a new company
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.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.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.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.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.paid_entry = DoubleVar()
Entry(top, textvariable = self.paid_entry).grid(row = 5, column = 1, padx = 5, pady = 5)
self.paid_entry.set("")
self.frames=[]
self.column=0
self.brokerage_main = DoubleVar()
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.current_main = DoubleVar()
self.total_two_main = DoubleVar()
self.loss_gain_main = DoubleVar()
self.frames.append(Frame(self.data_frame))
self.frames[-1].grid()
Label(self.frames[-1], textvariable=self.name_main).grid(row=self.rows, column=1)
Label(self.frames[-1], textvariable=self.code_main).grid(row=self.rows, column=2)
Label(self.frames[-1], textvariable=self.date_main).grid(row=self.rows, column=3)
Label(self.frames[-1], textvariable=self.quantity_main).grid(row=self.rows, column=4)
Label(self.frames[-1], textvariable=self.paid_main).grid(row=self.rows, column=1)
Label(self.frames[-1], textvariable=self.brokerage_main).grid(row=self.rows, column=6)
Label(self.frames[-1], textvariable=self.total_main).grid(row=self.rows, column=7)
Label(self.frames[-1], textvariable=self.current_main).grid(row=self.rows, column=8)
Label(self.frames[-1], textvariable=self.total_two_main).grid(row=self.rows, column=9)
Label(self.frames[-1], textvariable=self.loss_gain_main).grid(row=self.rows, column=10)
self.rows+=1
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[self.code_entry.get()]=self.frames[-1]
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 = self.current_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.code_list[self.code_entry_delete.get()].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()