patternpythonMinor
Displaying a scrolling stock exchange ticker in a window
Viewed 0 times
tickerscrollingexchangestockdisplayingwindow
Problem
I've written my first OOP program (194 lines including docstring and comments) in Python that uses Tkinter as GUI and Threads. The program shows a window and displays a scrolling stock exchange ticker in a line.
I've created the following classes:
The code seems very ugly to me. I would like some feedback about the code structure, especially about the use of OOP.
```
#!usr/bin/python3
# -- coding: UTF-8 --
from tkinter import *
import time
import threading
from random import randint as randint, uniform as randlimit
class AplicationTkinter(Frame):
"""
Class of tkinter.Frame subclass, Initializes the GUI
methods:
initGUI, draws the layout
scroll_ticker, inserts character by character in the Text widget
"""
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initGUI()
self.scroll_ticker()
def initGUI(self):
# changes the window icon
self.parent.iconbitmap("tabla.ico")
self.parent.title("Stock Exchange Ticker")
# fix a status bar at the bottom of the window, for future improvements
self.status_bar = Label(self.parent, text="", bd=1, relief=SUNKEN, anchor=W)
self.status_bar.pack(side=BOTTOM, fill=X)
# content Frame for entry, for future improvements
self.frm_1 = Frame(self.parent)
self.frm_1.pack()
self.var_entry = StringVar()
self.ent_1 = Entry(self.frm_1, textvariable=self.var_entry)
self.ent_1.pack()
self.var_entry.set("a default value")
str_ent_1 = self.ent_1.get()
# content LabelFrame to show the ticker scrolling line of text
self.lblfr_1 =
I've created the following classes:
- A class that draws the GUI layout with Tkinter library.
- A class for the tickers, with the data of each securities.
- A class for manage the stock exchange market and dispatch the data to the GUI.
- The Thread subclass for updating the stock exchange values every certain time interval.
The code seems very ugly to me. I would like some feedback about the code structure, especially about the use of OOP.
```
#!usr/bin/python3
# -- coding: UTF-8 --
from tkinter import *
import time
import threading
from random import randint as randint, uniform as randlimit
class AplicationTkinter(Frame):
"""
Class of tkinter.Frame subclass, Initializes the GUI
methods:
initGUI, draws the layout
scroll_ticker, inserts character by character in the Text widget
"""
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initGUI()
self.scroll_ticker()
def initGUI(self):
# changes the window icon
self.parent.iconbitmap("tabla.ico")
self.parent.title("Stock Exchange Ticker")
# fix a status bar at the bottom of the window, for future improvements
self.status_bar = Label(self.parent, text="", bd=1, relief=SUNKEN, anchor=W)
self.status_bar.pack(side=BOTTOM, fill=X)
# content Frame for entry, for future improvements
self.frm_1 = Frame(self.parent)
self.frm_1.pack()
self.var_entry = StringVar()
self.ent_1 = Entry(self.frm_1, textvariable=self.var_entry)
self.ent_1.pack()
self.var_entry.set("a default value")
str_ent_1 = self.ent_1.get()
# content LabelFrame to show the ticker scrolling line of text
self.lblfr_1 =
Solution
One suggestion is to change the
You also don't need to hard code "CHAR_UP", "CHAR_DOWN" toggles when those can be evaluated with the sign of
stock_market structure from a list to a dictionary. stock_market = {'GOOG': {'Value': 587.25, 'Change': 12.14},
'APPL': {'Value': 237.14, 'Change': 7.25}
}You also don't need to hard code "CHAR_UP", "CHAR_DOWN" toggles when those can be evaluated with the sign of
change in the dict.if stock_market['GOOG']['Change'] > 0.:
direction = 'CHAR_UP'
else:
direction = 'CHAR_DOWN'Code Snippets
stock_market = {'GOOG': {'Value': 587.25, 'Change': 12.14},
'APPL': {'Value': 237.14, 'Change': 7.25}
}if stock_market['GOOG']['Change'] > 0.:
direction = 'CHAR_UP'
else:
direction = 'CHAR_DOWN'Context
StackExchange Code Review Q#57243, answer score: 4
Revisions (0)
No revisions yet.