I have a list created as
self.statusListCtrl = wx.ListCtrl(self.panelUpper, -1, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
I add data to this list using
self.statusListCtrl.Append([datetime.datetime.now(),action,result])
When my process is all done I have a nice list showing things that were tried, the result of that attempt and a datetime stamp. Now what I want to do is output that to a text file. my problem is that I cant get the data from my listctrl correctly.
This is how I am trying to iterate through the list.
fileName = 'd:\ssplogs\sspInstaller.log'
FILE = open(fileName,"w")
for itemIdx in range(0,self.statusListCtrl.GetItemCount()):
line = str(self.statusListCtrl.GetItemText(itemIdx) + "\n")
print "line" , line
writeLine = line
FILE.write(writeLine)
FILE.close()
The output I am getting though is only my datetime stamp, which is the first column of my list. How do I get this so I see something like
datetime, action, result
Use this to get Row x Column data:
self.list.GetItem(row, col).GetText()
Working example:
import wx
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.list = wx.ListCtrl(self.panel, style=wx.LC_REPORT)
self.list.InsertColumn(0, "Date")
self.list.InsertColumn(1, "Action")
self.list.InsertColumn(2, "Result")
for a in range(5):
self.list.Append(["Date %d" % a, "Action %d" % a, "Result %d" % a])
self.sizer = wx.BoxSizer()
self.sizer.Add(self.list, proportion=1, flag=wx.EXPAND)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
# Save
for row in range(self.list.GetItemCount()):
print(", ".join([self.list.GetItem(row, col).GetText() for col in range(self.list.GetColumnCount())]))
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Related
I am trying to implement MVC using Python3.8. I have used this https://www.tutorialspoint.com/python_design_patterns/python_design_patterns_model_view_controller.htm Python2's example for practice.
But, I am receiving the following error:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
My code is as following:
model.py
import json
class Person:
def __init__(self, first = None, last = None):
self.first = first
self.last = last
def name(self):
return ('%s %s' %(self.first, self.last))
#classmethod
def getAll(self):
database = open('data.txt', 'r')
result = []
jsonList = json.loads(database.read())
for item in jsonList:
item = json.loads(item)
person = Person(item['first'], item['last'])
result.append(person)
return result
view.py
from model import Person
def showAllView(list):
print ('In our db we have %i users. Here they are:' % len(list))
for item in list:
print (item.name())
def startView():
print ('MVC - the simplest example')
print ('Do you want to see everyone in my db?[y/n]')
def endView():
print ('Goodbye!')
controller.py
from model import Person
import view
def showAll():
#gets list of all Person objects
people_in_db = Person.getAll()
return view.showAllView(people_in_db)
def start():
view.startView()
answer = input('Enter y or n')
if answer == 'y':
return showAll()
else:
return view.endView()
if __name__ == "__main__":
start()
Data.txt
[{
"first": "abc",
"last": "xyz"
}]
Please, guide me in this and help me find the error. Thanks in advance.
I have solved the problem myself. The main problem was loading JSON elements twice in model.py, like below:
jsonList = json.loads(database.read())
for item in jsonList:
item = json.loads(item)
Now I have solved it by removing item = json.loads(item).
I have simple code which creates two fields by the press of a button. There are two other buttons to save and load back the entry fields created. I have used the bind function to bind field A and field B. Pressing the Enter button on field A after entering a number will print out its value multiplied by 5 in field B. At this point the bind function works perfectly.
When I create three entry fields and save the progress without entering any inputs and compile the program, then load the file, the bind function does not seem to work. It seems to work only for the last field created. My code is as follows. I tried my best to simplify the code.
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfile
from tkinter import messagebox
import pickle
class Test(Frame):
def Widgets(self):
self.button_add = Button(self, text = "Add", command = self.add)
self.button_add.grid(row=0, column =2)
self.button_save = Button(self, text = "save", command = self.save)
self.button_save.grid(row=0, column =3)
self.button_load = Button(self, text = "load", command = self.load)
self.button_load.grid(row=0, column =4)
def add(self):
def test(event):
self.field_B[n].delete(0, END)
self.field_B[n].insert(0, (float(self.field_A[n].get()))*5)
self.field_A.append({})
n = len(self.field_A)-1
self.field_A[n] = Entry(self)
self.field_A[n].grid(row=n, column =0)
self.field_A[n].bind("<Return>", test)
self.field_B.append({})
n = len(self.field_B)-1
self.field_B[n] = Entry(self)
self.field_B[n].grid(row=n, column =1)
def save(self):
for n in range(len(self.field_A)):
self.entry_A.append(self.field_A[n].get())
self.entry_B.append(self.field_B[n].get())
fname = asksaveasfile(mode = "w", defaultextension = ".est")
data = {"fields": len(self.field_A), "entries_A": (self.entry_A),"entries_B": (self.entry_B)}
with open(fname.name, "wb") as file:
pickle.dump(data, file)
def load(self):
def test(event):
print("Why is the value of n always equal to", n, "?")
self.field_B[n].delete(0, END)
self.field_B[n].insert(0, (float(self.field_A[n].get()))*5)
fname = askopenfilename(filetypes = (("Estimation Files (est)", "*.est"),))
location = fname.replace("/", "\\")
if location:
with open(location, "rb") as file:
data = pickle.load(file)
for n in range(data["fields"]):
self.field_A.append({})
self.field_A[n] = Entry(self)
self.field_A[n].grid(row=n, column =0)
self.field_A[n].insert(0, data["entries_A"][n])
self.field_A[n].bind("<Return>", test)
self.field_B.append({})
self.field_B[n] = Entry(self)
self.field_B[n].grid(row=n, column =1)
self.field_B[n].insert(0, data["entries_B"][n])
def __init__(self,master = None):
Frame.__init__(self, master)
self.field_A = []
self.field_B = []
self.entry_A = []
self.entry_B = []
self.grid()
self.Widgets()
root = Tk()
app = Test(master = None)
app.mainloop()
You need a "closure". You can make a closure in python with the functools.partial function.
from functools import partial
def test(n, event=None):
self.field_B[n].delete(0, END)
self.field_B[n].insert(0, (float(self.field_A[n].get()))*5)
#other code ...
self.field_A[n].bind("<Return>", partial(test, n))
Both of your test() functions are accessing a variable n from the enclosing function. In the case of add(), there is no loop; n has a single value. Each Entry's test() gets its own n, because they were bound by a distinct call to add(). In load(), however, you are looping over n values; each test() is referring to the same n, which will have its final value by the time that any binding can possibly be invoked. The other answer gives a reasonable way to give each instance of test() its own personal n, so I'm not going to repeat that here.
I am using pyqtgraph to draw a candle stick like the following:
#-------------------
# Create pyqtgraph module
#-------------------
def GUI_DRAW_create():
"""
set default config
"""
pg.setConfigOption('background', 'w') #background: white
pg.setConfigOption('foreground', 'k') #font: black
class TimeAxisItem(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
#values is not my date in timestamp
return [datetime.fromtimestamp(value) for value in values]
## Create a subclass of GraphicsObject.
## The only required methods are paint() and boundingRect()
## (see QGraphicsItem documentation)
class CandlestickItem(pg.GraphicsObject):
def __init__(self, data):
pg.GraphicsObject.__init__(self)
self.data = data ## data must have fields: time, open, close, min, max
self.generatePicture()
def generatePicture(self):
## pre-computing a QPicture object allows paint() to run much more quickly,
## rather than re-drawing the shapes every time.
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('k'))
w = (self.data[1][0] - self.data[0][0]) / 3.
for (t, open, close, min, max) in self.data:
p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max))
if open > close:
p.setBrush(pg.mkBrush('r'))
else:
p.setBrush(pg.mkBrush('g'))
p.drawRect(QtCore.QRectF(t-w, open, w*2, close-open))
p.end()
# I try to print out t here, is my date
def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
## boundingRect _must_ indicate the entire area that will be drawn on
## or else we will get artifacts and possibly crashing.
## (in this case, QPicture does all the work of computing the bouning rect for us)
return QtCore.QRectF(self.picture.boundingRect())
class GUI_DRAW_new(QMainWindow):
def __init__(self):
super().__init__()
GUI_DRAW_create()
self.setWindowTitle("pyqtgraph example: PlotWidget")
cw = QWidget()
self.setCentralWidget(cw)
main_layout = QHBoxLayout()
cw.setLayout(main_layout)
#variable
self.signalgraph = None
self.data = []
self.vb = None
self.vLine = None
# define plot windows
self.GUI_DRAW_new_graph()
main_layout.addWidget(self.signalgraph)
self.signalgraph.setMouseTracking(True)
self.signalgraph.viewport().installEventFilter(self)
self.show()
def eventFilter(self, source, event):
try:
if (event.type() == QtCore.QEvent.MouseMove and
source is self.signalgraph.viewport()):
pos = event.pos()
print('mouse move: (%d, %d)' % (pos.x(), pos.y()))
if self.signalgraph.sceneBoundingRect().contains(pos):
mousePoint = self.vb.mapSceneToView(pos)
index = int(mousePoint.x())
int(index)
#if index > 0 and index < len(self.data):
#print(self.xdict[index])
# self.label.setHtml("<p style='color:black'>日期:{0}</p>".format(self.data[index]))
# self.label.setPos(mousePoint.x(),mousePoint.y())
self.vLine.setPos(mousePoint.x())
return QtGui.QWidget.eventFilter(self, source, event)
except Exception as e:
traceback.print_exc()
err = sys.exc_info()[1]
PRINT_DEBUG(0,str(err))
def GUI_DRAW_new_graph(self):
try:
self.signalgraph = pg.PlotWidget(name="Signalgraph", axisItems={'bottom': TimeAxisItem(orientation='bottom')})
# sample data
self.data = [ ## fields are (time, open, close, min, max).
(1514995200.0, 102.610001, 105.349998, 102, 105.370003),
(1515081600.0, 105.75, 102.709999, 102.410004, 105.849998),
(1515168000.0, 100.559998, 102.370003, 99.870003, 100.699997),
(1515254400.0, 98.68, 96.449997, 96.43, 100.129997),
(1515340800.0, 98.550003, 96.959999, 96.760002, 99.110001),
(1515427200.0, 102.610001, 105.349998, 102, 105.370003),
(1515513600.0, 105.75, 102.709999, 102.410004, 105.849998),
(1515600000.0, 100.559998, 102.370003, 99.870003, 100.699997),
(1515686400.0, 98.68, 96.449997, 96.43, 100.129997),
(1515772800.0, 98.550003, 96.959999, 96.760002, 99.110001),
]
#if comment this 2 code, can see the string
item = CandlestickItem(self.data)
self.signalgraph.addItem(item)
#trick
s_day = datetime.fromtimestamp(self.data[0][0]).strftime("%Y-%m-%d")
e_day = datetime.fromtimestamp(self.data[len(self.data) - 1][0]).strftime("%Y-%m-%d")
tr=np.arange(s_day, e_day, dtype='datetime64') # tick labels one day
tday0=(tr-tr[0])/(tr[-1]-tr[0]) #Map time to 0.0-1.0 day 2 1.0-2.0 ...
tday1=tday0+1
tnorm=np.concatenate([tday0,tday1])
tr[-1]=tr[0] # End day=start next day
ttick=list()
for i,t in enumerate(np.concatenate([tr,tr])):
tstr=np.datetime64(t).astype(datetime)
ttick.append( (tnorm[i], tstr.strftime("%Y-%m-%d")))
ax=self.signalgraph.getAxis('bottom') #This is the trick
ax.setTicks([ttick])
#cross hair in signalgraph
self.vLine = pg.InfiniteLine(angle=90, movable=False)
self.signalgraph.addItem(self.vLine, ignoreBounds=True)
self.vb = self.signalgraph.plotItem.vb
except Exception as e:
traceback.print_exc()
err = sys.exc_info()[1]
print(0,str(err))
# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == "__main__":
app = QtGui.QApplication([])
gui = GUI_DRAW_new()
app.exec_()
The result is:
candlestick_graph_center
I want to set the x-axis interval using the date: [2018-01-04, 2018-01-05, 2018-01-06, ....].
Thanks for help, many thanks
Update
I figure out that why all data squeeze together, because of the TextItem.
So that, I rewrite the code.
I try to use tickStrings in AxisItem to convert value to string, when I print out the values in tickStrings, it is not my data value (date in timestamp). Why the values are different? Thanks a lots
Update
If I use setTicks with candle stick, it cannot show string in the graph. Only can show the string without candle stick.
Any idea?
You have to convert into a string with the appropriate format using strftime():
from datetime import datetime
class TimeAxisItem(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
#values is not my date in timestamp
return [datetime.fromtimestamp(value).strftime("%Y-%m-%d") for value in values]
Or:
from datetime import date
class TimeAxisItem(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
#values is not my date in timestamp
return [date.fromtimestamp(value) for value in values]
Or:
from datetime import datetime
class TimeAxisItem(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
#values is not my date in timestamp
return [datetime.fromtimestamp(value).date() for value in values]
The line self.tableView.setSortingEnabled(True) sorts a table view when clicking on the header, but it sorts incorrectly. That is, it thinks every column is a string (e.g. it sorts numbers like 1,11,12,2,22,3, etc). How do I correct this?
My code:
self.model = QtGui.QStandardItemModel()
with open(file_name_temp, "rt") as fileInput:
i = 1
for row in csv.reader(fileInput):
item = QtGui.QStandardItem()
for field in row:
items = [
item.setData(field, QtCore.Qt.UserRole)
]
print(items)
self.model.appendRow(items)
tab_table_view = QtGui.QWidget()
self.Tab.insertTab(0, tab_table_view, self.File_Name)
self.tableView = QtGui.QTableView(tab_table_view)
self.tableView.setGeometry(QtCore.QRect(0, 0, 721, 571))
self.model = QtGui.QStandardItemModel(self)
self.tableView.setModel(self.model)
colll = self.Datas.dtypes.index
col_names = np.array(colll)
col_names = np.insert(col_names, 0, self.Datas.index.name)
self.model.setHorizontalHeaderLabels(col_names)
self.tableView.hideRow(0)
self.model.setSortRole(QtCore.Qt.UserRole)
Update 1:
if (".csv" or ".txt") in self.File_Name:
with open(file_name_temp, "rt") as fileInput:
i = 1
reader = csv.reader(fileInput)
next(reader, None)
for row in reader:
for x in range(0,Num_col+1):
try:
int(row[x])
row[x]=int(row[x])
except ValueError:
print('Not Int')
items = []
for field in row:
item = QtGui.QStandardItem(field)
if type(field)==int:
print('yyy')
data = int(field)
else:
data = field
item.setData(data, QtCore.Qt.UserRole)
items.append(item)
print(items)
self.model.appendRow(items)
gives the output as:
yyy
yyy
yyy
yyy
yyy
yyy
yyy
yyy
yyy
[<PyQt4.QtGui.QStandardItem object at 0x0000000006DF3948>, <PyQt4.QtGui.QStandardItem object at 0x0000000006DF38B8>, <PyQt4.QtGui.QStandardItem object at 0x0000000006DF3828>, <PyQt4.QtGui.QStandardItem object at 0x0000000006DF3798>, <PyQt4.QtGui.QStandardItem object at 0x0000000006DF3678>, <PyQt4.QtGui.QStandardItem object at 0x0000000006DF3EE8>, <PyQt4.QtGui.QStandardItem object at 0x0000000006DF3F78>, <PyQt4.QtGui.QStandardItem object at 0x00000000095D4048>, <PyQt4.QtGui.QStandardItem object at 0x00000000095D40D8>]
everything seems good in console but on the GUI window it does not show the table?
You don't show how you are creating the items for the model, but presumably you are doing something like this:
item = QtGui.QStandardItem(str(value))
where value is a python numeric type.
To get numeric sorting, set the values like this instead:
item = QtGui.QStandardItem()
item.setData(value, QtCore.Qt.DisplayRole)
But note that this will also make the table automatically use spin-boxes for editing cells, which you may not want. So an alternative solution would be:
item = QtGui.QStandardItem(str(value))
item.setData(value, QtCore.Qt.UserRole)
...
model.setSortRole(QtCore.Qt.UserRole)
Finally, for fully customised sorting, you can also subclass QStandardItem:
class StandardItem(QtGui.QStandardItem):
def __lt__(self, other):
return int(self.text()) < int(other.text())
item = StandardItem(str(value))
UPDATE:
Here is a demo script that reads csv files into a table, automatically converting the fields into the correct data-type for sorting:
import sys, csv
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.model = QtGui.QStandardItemModel(self)
self.model.setSortRole(QtCore.Qt.UserRole)
self.tableView = QtGui.QTableView()
self.tableView.setSortingEnabled(True)
self.tableView.setModel(self.model)
self.button = QtGui.QPushButton('Open CSV', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.tableView)
layout.addWidget(self.button)
def handleButton(self):
path = QtGui.QFileDialog.getOpenFileName(
self, 'Open CSV', '', 'CSV files (*.csv *.txt)')
if path:
self.model.setRowCount(0)
with open(path) as stream:
reader = csv.reader(stream)
next(reader, None)
for row in reader:
items = []
for field in row:
item = QtGui.QStandardItem(field)
for numtype in (int, float):
try:
data = numtype(field)
break
except (ValueError, OverflowError):
pass
else:
print('Not a number: %r' % field)
data = field
item.setData(data, QtCore.Qt.UserRole)
items.append(item)
self.model.appendRow(items)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 150, 600, 400)
window.show()
sys.exit(app.exec_())
I want to know where and what was changed by user in tkinter's Text widget.
I've found how to get that text was somehow modified by using <<Modified>>event but I can't get actual changes:
from tkinter import *
def reset_modified():
global resetting_modified
resetting_modified = True
text.tk.call(text._w, 'edit', 'modified', 0)
resetting_modified = False
def on_change(ev=None):
if resetting_modified: return
print ("Text now:\n%s" % text.get("1.0", END))
if False: # ????
print ("Deleted [deleted substring] from row %d col %d")
if False: # ????
print ("Inserted [inserted substring] at row %d col %d")
reset_modified()
resetting_modified = False
root = Tk()
text = Text(root)
text.insert(END, "Hello\nworld")
text.pack()
text.bind("<<Modified>>", on_change)
reset_modified()
root.mainloop()
For example, if I select 'ello' part from "hello\nworld" in Text widget then I press 'E', then I want to see
"Deleted [ello] from row 0 col 1" followed by "Inserted [E] at row 0 col 1"
is it possible to get such changes (or at least their coordinates) or I have basically to diff text on each keystroke if I want to detect changes run time?
Catching the low level inserts and deletes performed by the underlying tcl/tk code is the only good way to do what you want. You can use something like WidgetRedirector or you can do your own solution if you want more control.
Writing your own proxy command to catch all internal commands is quite simple, and takes just a few lines of code. Here's an example of a custom Text widget that prints out every internal command as it happens:
from __future__ import print_function
import Tkinter as tk
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
"""A text widget that report on internal widget commands"""
tk.Text.__init__(self, *args, **kwargs)
self._orig = self._w + "_orig"
self.tk.call("rename", self._w, self._orig)
self.tk.createcommand(self._w, self.proxy)
def proxy(self, command, *args):
# this lets' tkinter handle the command as usual
cmd = (self._orig, command) + args
result = self.tk.call(cmd)
# here we just echo the command and result
print(command, args, "=>", result)
# Note: returning the result of the original command
# is critically important!
return result
if __name__ == "__main__":
root = tk.Tk()
CustomText(root).pack(fill="both", expand=True)
root.mainloop()
After digging around, I've found that idlelib has WidgetRedirector which can redirect on inserted/deleted events:
from tkinter import *
from idlelib.WidgetRedirector import WidgetRedirector
def on_insert(*args):
print ("INS:", text.index(args[0]))
old_insert(*args)
def on_delete(*args):
print ("DEL:", list(map(text.index, args)))
old_delete(*args)
root = Tk()
text = Text(root)
text.insert(END, "Hello\nworld")
text.pack()
redir = WidgetRedirector(text)
old_insert=redir.register("insert", on_insert)
old_delete=redir.register("delete", on_delete)
root.mainloop()
Though it seems hacky. Is there a more natural way?