【发布时间】:2021-12-24 07:15:54
【问题描述】:
您好,我一直在尝试使用 tkinter 和 python 创建我的小项目,但我尝试了所有方法在 frame 中添加 scrollbar,但不幸的是我无法做到。
谁能帮帮我?
from tkinter import *
from tkinter import messagebox
import urllib.error, urllib.parse ,urllib.request
from bs4 import BeautifulSoup
b="#0c1231"
root = Tk()
root.title("Stock World")
root.geometry("1350x722+5+5")
root.configure(bg=b)
fr = Frame(root)
fr.configure(bg=b)
fr.place(relx=0.01,rely=0.11,width=1310,height=635)
我一直在尝试通过单击从网站和不同页面获取股票访问权限。
def get_web_result():
for i in range(1,2):
try:
lnk="https://www.moneycontrol.com/broker-research/latestResearchReport/"+str(i)
url = urllib.request.urlopen(lnk)
except:
messagebox.showwarning("Notification","Check your internet connection and try again later !!!")
bs = BeautifulSoup(url,"html.parser")
tg = bs('tbody')
for line in tg:
lne = line.text
fopen = open("stock.txt","w")
f = fopen.writelines(lne.strip())
i=i+1
fopen = open("stock.txt","r")
fopen = fopen.readlines()
x=0.01
y=0.02
a=1
for l in fopen:
if len(l)>0:
if a==1: #date
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.005,rely=y)
elif a==2: #company
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.09,rely=y)
elif a==3: #broker
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.20,rely=y)
elif a==4: #buy or sell
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.31,rely=y)
elif a==5: #report price
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.38,rely=y)
elif a==6: #cmp
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.475,rely=y)
elif a==7: #target price
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.56,rely=y)
elif a==8: #profit
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.62,rely=y)
elif a==9: #realized return
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.70,rely=y)
elif a==10: #sensex return
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.81,rely=y)
elif a==11: #
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.98,rely=y)
elif a==12: #
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.99,rely=y)
elif a==13: #
lb = Label(fr,text=l, font=("arial",10),fg="white",bg=b)
lb.place(relx=0.99,rely=y)
else:
pass
a=a+1
if a == 14:
y=y+0.03
a=1
以上代码在tkinter窗口上显示输出。
lf = "{Arial} 13 bold"
money_ctrl = Button(root,text = "Get Result....!!!",font=("Bahnschrift",13,"bold"),fg="white",bg="#363ddb",command = get_web_result)
money_ctrl.place(relx=0.01,rely=0.01,width=490)
date_lb =Label(root,text="Date",font=lf ,fg="#ffc202",bg=b)
date_lb.place(relx=0.03,rely=0.075)
stockname_lb =Label(root,text="Company",font=lf ,fg="#ffc202",bg=b)
stockname_lb.place(relx=0.10,rely=0.075)
broker_lb =Label(root,text="Broker",font=lf ,fg="#ffc202",bg=b)
broker_lb.place(relx=0.21,rely=0.075)
bors_lb =Label(root,text="Buy/Sell",font=lf ,fg="#ffc202",bg=b)
bors_lb.place(relx=0.30,rely=0.075)
prs_lb =Label(root,text="Report Price",font=lf ,fg="#ffc202",bg=b)
prs_lb.place(relx=0.37,rely=0.075)
cmpp_lb =Label(root,text="CMP",font=lf ,fg="#ffc202",bg=b)
cmpp_lb.place(relx=0.47,rely=0.075)
tgt_lb =Label(root,text="Target price",font=lf ,fg="#ffc202",bg=b)
tgt_lb.place(relx=0.53,rely=0.075)
pf_lb =Label(root,text="Profit %",font=lf ,fg="#ffc202",bg=b)
pf_lb.place(relx=0.62,rely=0.075)
rr_lb =Label(root,text="Realized Return",font=lf ,fg="#ffc202",bg=b)
rr_lb.place(relx=0.68,rely=0.075)
ss =Label(root,text="Sensex Return",font=lf ,fg="#ffc202",bg=b)
ss.place(relx=0.79,rely=0.075)
root.mainloop()
谢谢。
【问题讨论】:
-
您不能使用
scrollbar滚动Frame。您只能将Frame放在Canvas上并滚动Canvas。但它需要更多的代码。见 GitHub:scrolled-frame-canvas/ -
@furas:您还可以滚动
Text小部件。 -
@BryanOakley 你说得对,我忘了
Text也可以用create_window加Label或Frame
标签: python python-3.x tkinter scrollbar tkinter-layout