Tkinter如何打印标签

时间:2018-03-21 22:44:47

标签: python tkinter

我是GUI和Tkinter的新手,但我基本上想要做的是有一个平台,只需点击一下按钮,我就可以使用请求和BS4模块获得股价和相关的股票信息。我使用的是Python 2.7。

当我点击GUI上的按钮时,股价在我的标签上完美显示。但是,它会在其下方的标签中显示异常信息,而不是它应显示%和货币每日移动。原因是我已经将GUI python文件链接到我用来“下载”价格的另一个python文件,并且我已经在函数中包含了向上或向下的价格变动。

因此,当我调用此函数的结果打印在标签中时 - 它们被打印出来但是带有“function share_movement at 0x022B34F0”的内容

下面是我的GUI代码,然后是我的其他python文件的代码:

import Tkinter as tk
import Share_price_info as kio_file

#Creating the parent window
root = tk.Tk()
f = tk.Frame(root)

#Title for window
root.title("My first GUI")

#Size of the window - min and max
root.maxsize(850, 850)
root.minsize(250, 250)

#Setting a fixed size of window on opening
root.geometry("800x750")

#Setting the background colour
root.configure(background="white")

title = tk.Label(root, text="Platform", fg="black", font="Helvetica 20 bold", bg="grey")
title.grid(row=0, columnspan=3)

#Logo for top
topimg = tk.PhotoImage(file="stock_market.gif")
stock_img = tk.Label(root, image=topimg, bg="black")
stock_img.grid(row=1, columnspan=3)

#Entry to type which share you want to find info for
search = tk.Entry(root)
search.grid(row=2, column=1, sticky="W")

#Function to print out what has been searched for in the Entry bar
def search_bar():
    print search.get()
    kio_file.share_price()
    #if search.get() == "kio":
        #use the information from the share info script
        #kio_file.share_price()

#Search button
tk.Button(root, text="Search", command=search_bar) .grid(row=3, column=1, sticky="W")

#Button when pressed closes program.
exit_btn = tk.Button(root, text="Exit Platform", bg="black", fg="white", width=20, height=2, command=root.destroy)
exit_btn.grid(row=4, column=2, sticky="E")   


#Label that prints the share info when the "KIO info" button is clicked
currentPrice = tk.StringVar(root, "Click for share info")
currentMovement = tk.StringVar(root, "Click for share info")

def setValues():
    price = kio_file.kio_price
    print price
    currentPrice.set(price)
    movement = kio_file.share_movement
    print movement
    currentMovement.set(movement)

#When button is clicked, the function as part of the "command" widget in the button code will run and the "textvariable" here
#which is linked to it will also be activated.
kio_printout = tk.Label(root, textvariable=currentPrice, bg='black', fg="white", width=40, height=3)
kio_printout.grid(row=4, column=1, padx=5, pady=5)

kio_printout = tk.Label(root, textvariable=currentMovement, bg='black', fg="white", width=40, height=3)
kio_printout.grid(row=5, column=1, padx=5, pady=5)

#When push the button the share details print out
kio = tk.Button(root, text="KIO info", bg="black", fg="white", width=30, height=2, command=setValues)
kio.grid(row=4, column=0, sticky="W", padx=5, pady=5)


print root.grid_size()

#Run the program
root.mainloop()

我试图删除我的代码中的所有不必要的注释,除了那些可能有点帮助的注释。

另一个python文件

import os
import pandas as pd
import numpy
import matplotlib
from bs4 import BeautifulSoup as soup     #This parses the html text
import requests

#Get the current working directory
os.getcwd()
os.path.isfile("KIO.csv")

#Read the csv file with the share prices
df = pd.read_csv("KIO.csv")

#Average closing price for full year
average_close = closing.mean()
print average_close

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
#ping the website
r = requests.get("https://finance.yahoo.com/quote/KIO.JO?p=KIO.JO", headers=headers)

#Opens the connection and downloads the webpage
kio_site = urllib2.urlopen("https://finance.yahoo.com/quote/KIO.JO?p=KIO.JO")

#This will print all the html on the webpage
#kio_html = kio_site.read()

#Now closing the internet connection that you opened before
kio_site.close()

#now you want to parse the html file
page_soup = soup(r.text, "html.parser")

#print the html webpage to be properly nested:
#print page_soup.prettify()

#Specifically find certain elements
kio_price = page_soup.find("span", {"class": "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)"}).text

def share_movement():
    try:
        print page_soup.find("span", {"class": "Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($dataGreen)"}).text
    except:
        print page_soup.find("span", {"class": "Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($dataRed)"}).text

问题基本上是第一个文件中的“setValues”函数,然后第二个文件中的“share_movement”函数,因为我不认为Label知道要打印哪个。但是,我需要像这样编码,以便考虑每天上下价格。

非常感谢你提前帮忙!

0 个答案:

没有答案