I want to design a wxpython ListCtrl. So when the Search button is clicked i am getting list like this
[(7, u'GLUOCOSE', u'C6H1206'), (8, u'SUCROSE', u'C12H22O11')]
I want to populate the listctrl with the above output. I am InsertStringItem method, which will return me the index of current row and rest of the columns are filled by SetStringItem() method.But that gives me TypeError: String or Unicode type required.How do i accomplish this?
def OnSearch(self, event):
placeholder = '?'
placeholders = ','.join(placeholder for unused in self.molecule_list)
query = 'SELECT * FROM MOLECULE WHERE MOL_NUMBER IN (%s)' % placeholders
cursor = self.conn.execute(query, self.molecule_list)
final = cursor.fetchall()
print final
for j in final:
index = self.list.InsertStringItem(sys.maxint, j[0])
self.list.SetStringItem(index, 1, j[1])
self.list.SetStringItem(index, 2, j[2])
You are iterating over the cursor, rather than the data which was returned in the variable final. Swap over to iterate using final
index = 0
for j in final:
index = self.list.InsertStringItem(index, str(j[0]))
self.list.SetStringItem(index, 1, j[1])
self.list.SetStringItem(index, 2, j[2])
index +=1
The easiest way, is to ensure that the ListCtrl has a style=wx.LC_REPORT and then use Append.
for j in final:
self.list.Append((j[0],j[1],j[2],j[3]))
Where j[n] is each item you require from the data for the number of items in the ListCtrl. I hope that makes sense.
Here is an example showing both methods
import wx
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Molecules")
panel = wx.Panel(self, wx.ID_ANY)
self.index = 0
self.list_ctrl = wx.ListCtrl(panel, size=(-1,100),
style=wx.LC_REPORT
)
self.list_ctrl.InsertColumn(0, 'Number')
self.list_ctrl.InsertColumn(1, 'Element')
self.list_ctrl.InsertColumn(2, 'Make up')
btn = wx.Button(panel, label="Add data")
btn.Bind(wx.EVT_BUTTON, self.add_lines)
btn2 = wx.Button(panel, label="Append data")
btn2.Bind(wx.EVT_BUTTON, self.app_lines)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(btn2, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
def add_lines(self, event):
data = [[7, 'GLUCOSE', 'C6H1206'],[8,'SUCROSE', 'C12H22O11']]
index = 0
for j in data:
self.list_ctrl.InsertStringItem(index, str(j[0]))
self.list_ctrl.SetStringItem(index, 1, j[1])
self.list_ctrl.SetStringItem(index, 2, j[2])
index += 1
def app_lines(self, event):
data = [[7, 'GLUCOSE', 'C6H1206'],[8,'SUCROSE', 'C12H22O11']]
for j in data:
self.list_ctrl.Append((j[0],j[1],j[2]))
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
Related
I have a Qtablewidget with 3 columns. The first column contain user names, the second and third are columns are check boxes. I would like these check boxes to be attributes for the row of users. For example If you look at the image below, User: bruno is marked(has attributes) delete and delete home. I would like to have an output like: User Bruno marked for delete, marked for delete home. To do that, I would need to link the users to both these columns which I haven't the slightest idea. How should I approach this problem. Below is the code that I already came up with. It populates the users column from a file.
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self, rows, columns):
QtGui.QWidget.__init__(self)
self.table = QtGui.QTableWidget(rows, columns, self)
self.table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("Users"))
self.table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("Delete User"))
self.table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("Delete User and Home"))
self.table.verticalHeader().hide()
header = self.table.horizontalHeader()
header.setStretchLastSection(True)
rowf = 0
with open("/home/test1/data/users") as in_file:
if in_file is not None:
users = in_file.readlines()
for line in users:
users = QtGui.QTableWidgetItem()
self.table.setItem(rowf, 0, users)
users.setText(line)
rowf+=1
in_file.close()
for column in range(columns):
for row in range(rows):
if column % 3:
item = QtGui.QTableWidgetItem(column)
item.setFlags(QtCore.Qt.ItemIsUserCheckable |
QtCore.Qt.ItemIsEnabled)
item.setCheckState(QtCore.Qt.Unchecked)
self.table.setItem(row, column, item)
self.table.itemClicked.connect(self.cell_was_clicked)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.table)
self._list = []
def cell_was_clicked(self, item):
#row = self.table.currentItem().row()
#col = self.table.currentItem().column()
#print "item:", item
#print "row=", row
if item.checkState() == QtCore.Qt.Checked:
print('"%s" Checked' % item.text())
#self._list.append(item.row())
else:
#print "(", row , ",", col, ")"
print('%s' % item.text())
print (item.row(),item.column())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window(200, 3)
window.resize(400, 400)
window.show()
sys.exit(app.exec_())
fixed it like this:
def cell_was_clicked(self, item):
if item.checkState() == QtCore.Qt.Checked:
user = self.table
delete = user.horizontalHeaderItem(item.column()).text()
print ('%s' % user.item(item.row(), 0).text())
print ('is marked for %s' % delete)
print('%s Checked' % item.text())
#self._list.append(item.row())
else:
print('%s' % item.text())
print (item.row(),item.column())
I have a tkinter class which reads some data into a couple of lists. From this now i have created a dictionary for creating checkbuttons.
I'm trying to create those checkbuttons in a new window() with a button to submit and read the stutus of those. I want this data to process.
def get_data(self):
self.flags = ["one","two","three", "four"]
self.tests = ["Jack","Queen","King","Ace"]
self.value = [11,12,13,1]
self.dict1 = {k:v for k,v in enumerate(self.flags,1)}
def get_status(self):
self.selectWindow = Toplevel(root)
self.selectWindow.title("Select Test Cases")
Submit_btn = Button(selectWindow, text="Submit", command=read_status )
for testcase in self.dict1:
self.dict1[testcase] = Variable()
l = Checkbutton(self.selectWindow,text=self.dict1[testcase], variable=self.dict1[testcase])
l.pack()
def read_status(self):
pass
From here I'm not able go ahead and read the status of checkbuttons and get those are checked. I need this data for further processing on tests(not actual lists given here I have few more). How to solve? Please let me know.
Checkbutton has a built in command function that can solve this problem. Every time you press a button that function is called, and you can print out the values of the buttons (0,1)
def get_data(self):
self.flags = ["one","two","three", "four"]
self.tests = ["Jack","Queen","King","Ace"]
self.value = [11,12,13,1]
self.dict1 = {k:v for k,v in enumerate(self.flags,1)}
def get_status(self):
self.selectWindow = Toplevel(self)
self.selectWindow.title("Select Test Cases")
self.get_data()
Submit_btn = Button(self.selectWindow, text="Submit", command=read_status ) # This button should be packed
Submit_btn.pack()
for testcase in self.dict1:
self.dict1[testcase] = Variable()
l = Checkbutton(self.selectWindow,text=self.dict1[testcase], variable=self.dict1[testcase], command=self.read_status) # Note the command
l.pack()
self.selectWindow.mainloop()
# Here comes the interesting part
def read_status(self):
for i,j in self.dict1.iteritems():
print j.get()
You forgot to use self and pack method:
Submit_btn = Button(self.selectWindow, text="Submit", command=self.read_status )
Submit_btn.pack()
Checkbutton's states are (0, 1) so use IntVar() to inspect the state:
...
self.dict1[testcase] = IntVar()
...
Then use IntVar get method:
def read_status(self):
for v in self.dict1:
print self.dict1[v].get()
I have 2 listboxes (which are connected so that items can move from one to the other) and at the end I would like to get all the entries in the second listbox by using a 'Ok' button (or simply closing the frame). I could add/remove values to a list every time an item is selected (as shown in the commented section of the code below) but I would rather have a single line of code along the lines of [master.selected.get(idx) for idx in master.selected.curselection()] in the close function but I am unable to get it working.
Code:
def measurementPopup(self,master):
self.chargeCarrier = StringVar()
self.massModifiers = StringVar()
self.chargeCarrier.set("[M+xH]")
def onselect1(evt):
w = evt.widget
index = int(w.curselection()[0])
value = w.get(index)
# My Dirty fix -> Here I could enter the selected value to a buffer list (to be returned in the ok function).
master.selected.insert(END,value)
master.avail.delete(index)
def onselect2(evt):
w = evt.widget
index = int(w.curselection()[0])
value = w.get(index)
# My Dirty fix -> Here I could remove the selected value from a buffer list (to be returned in the ok function).
master.selected.delete(index)
master.avail.insert(END,value)
def close(self):
# Here I would return the buffer list and close the window
master.measurementWindow = 0
top.destroy()
if master.measurementWindow == 1:
return
master.measurementWindow = 1
top = self.top = Toplevel()
top.protocol( "WM_DELETE_WINDOW", lambda: close(self))
self.charge = Label(top, text = "Charge", width = 10)
self.charge.grid(row = 0, column = 0, sticky = W)
self.min = Label(top, text = "Min", width = 5)
self.min.grid(row=0, column = 1, sticky = W)
self.minCharge = Spinbox(top, from_= 1, to = 3, width = 5)
self.minCharge.grid(row = 0, column = 2, sticky = W)
self.max = Label(top, text = "Max", width = 5)
self.max.grid(row = 0, column = 3, sticky = W)
self.maxCharge = Spinbox(top, from_ = 1, to=3, width=5)
self.maxCharge.grid(row = 0, column = 4, sticky = W)
self.chargeCarrier = OptionMenu(top, self.chargeCarrier, "[M+xH]", "[M+xNa]")
self.chargeCarrier.grid(row = 0, column = 5, sticky = W)
self.availMass = Label(top, text = "Available")
self.availMass.grid(row = 1, column = 1, sticky = W)
self.selectMass = Label(top, text = "Selected")
self.selectMass.grid(row = 1, column = 3, sticky = W)
self.massMod = Label(top, text = "Mass Mods")
self.massMod.grid(row = 2, column = 0, sticky = W)
self.avail = Listbox(top)
for i in UNITS:
if BLOCKS[i]['available'] == 1:
self.avail.insert(END,BLOCKS[i]['human_readable_name'])
self.avail.grid(row = 2, column = 1, columnspan = 2, sticky = W)
self.avail.bind('<<ListboxSelect>>',onselect1)
self.selected = Listbox(top)
self.selected.grid(row = 2, column = 3, columnspan = 2, sticky = W)
self.selected.bind('<<ListboxSelect>>',onselect2)
self.ok = Button(top,text = 'Ok',command = lambda: close(self))
self.ok.grid(row = 3, column = 0, sticky = W)
I have tried to use the following small snippet in the close function:
values = [master.selected.get(idx) for idx in master.selected.curselection()]
print ', '.join(values)
However, the for segment doesn't return anything. I would expect that this is due to the fact that nothing is actually selected but that I would need something opposite, along the lines of master.selected.allitems() (if it exists and if I understand it correctly).
Summary
How would one get all the items in 1 specific listbox?
The .get() function for the Listbox widget allows you to specify a range of items, which can be specified as 0 to END to return a tuple of all the items.
Example:
from Tkinter import *
root = Tk()
l = Listbox(root, width = 15)
l.pack()
l.insert(END, "Hello")
l.insert(END, "world")
l.insert(END, "here")
l.insert(END, "is")
l.insert(END, "an")
l.insert(END, "example")
def close():
global l, root
items = l.get(0, END)
print(items)
root.destroy()
b = Button(root, text = "OK", command = close).pack()
root.mainloop()
I hope this helps, if it's not what you were looking for let me know in a comment and I can try expand my answer.
I've written a Cryptoquote Generator in Python using wxPython. I don't really have any problems with wxPython itself, but rather with resetting my variables. My program works this way: I have a button that generates an encrypted quote. Then, the user has a decode button they press to change one letter at a time. There is a button to reset the whole quote and a button to reset changes one step back at a time.
I have a base_copy variable to store the original encrypted quote. It is an empty list that is populated with the individual characters of the encrypted quote one at a time when on_generate_quote is called. It remains unchanged throughout the loop -- so that I may call on it with on_clear_all or on_clear_last to reset my encrypted quote. The problem is, if I use my decode_button to decode a letter, then use my clear_all_button, then decode_button again, my clear_all_button calls on a base_copy that has now been somehow tainted with changed letters that should only be in my split_cryptoquote copy. Why is this, since I never make an implicit call to alter base_copy? (I do, however, make a call in on_clear_all to set split_cryptoquote to base_copy, but this shouldn't change base_copy.)
#/------------------ wxPython GUI -----------------\
class MainWindow(wx.Frame):
quote = []
quote.append(quote_fetch(quotes))
split_cryptoquote = []
base_copy = []
split_buffer = []
buffer_origin = None
buffer_replace = None
quote_altered = False #Becomes true after first decoding change.
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title="Cryptogrammar", size=(1000, 200))
self.CreateStatusBar()
self.txt = wx.StaticText(self, -1, "".join(MainWindow.split_cryptoquote), (20,30), (40,40))
self.txt.SetForegroundColour("WHITE")
#Menu
filemenu = wx.Menu()
menu_about = filemenu.Append(wx.ID_ABOUT, "&About", " Information about this program")
menu_how = filemenu.Append(HOW_TO, "&How to Play", " How to play Cryptogrammar")
menu_exit = filemenu.Append(wx.ID_EXIT,"E&xit", " Close Cryptogrammar")
#menuGenerate = filemenu.Append(wx.ID_NONE, "&Generate New", "Generate a new cryptogram")
#menu_bar
menu_bar = wx.MenuBar()
menu_bar.Append(filemenu, "&File")
self.SetMenuBar(menu_bar)
#Buttons
generate_button = wx.Button(self, -1, "&Generate Cryptogram")
decode_button = wx.Button(self, -1, "&Decode Letter")
clear_all_button = wx.Button(self, -1, "&Clear All Changes")
clear_last_button = wx.Button(self, -1, "Clear &Last Change")
answer_button = wx.Button(self, -1, "Show &Answer")
but_list = [generate_button, decode_button, clear_all_button, clear_last_button, answer_button]
#Sizers
self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
for i in range(0, 5):
self.sizer2.Add(but_list[i], 1, wx.EXPAND)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.txt, 1, wx.EXPAND)
self.sizer.Add(self.sizer2, 0, wx.EXPAND)
#Events
self.Bind(wx.EVT_MENU, self.on_about, menu_about)
self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)
self.Bind(wx.EVT_MENU, self.on_how, menu_how)
self.Bind(wx.EVT_BUTTON, self.on_generate_quote, generate_button)
self.Bind(wx.EVT_BUTTON, self.on_decode, decode_button)
self.Bind(wx.EVT_BUTTON, self.on_answer, answer_button)
self.Bind(wx.EVT_BUTTON, self.on_clear_all, clear_all_button)
self.Bind(wx.EVT_BUTTON, self.on_clear_last, clear_last_button)
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
self.SetTitle("Cryptogrammar")
self.Centre()
def on_about(self, e):
dialogue = wx.MessageDialog(self, "A program for generating random cryptograms.\n\n\n\nCopyright 2014 Joshua Simmons\nVersion 0.1.0", "About Cryptogrammar", wx.OK)
dialogue.ShowModal()
dialogue.Destroy()
def on_exit(self, e):
self.Close(True)
def on_how(self, e):
dialogue = wx.MessageDialog(self, "HOW TO PLAY:\n\n\n--\tPress the 'Generate Cryptogram' to spawn a cryptogram.\n\n--\tUse the 'Decode Letter' to replace an encrypted letter with a letter of your choice. 'Decoded' letters will be lowercase to distinguish them.\n\n--\tUse the 'Clear Changes' button to reset the puzzle.\n\n--\t'Show Answer' solves the puzzle!", "How to play Cryptogrammar", wx.OK)
dialogue.ShowModal()
dialogue.Destroy()
def on_decode(self, e):
dialogue = wx.TextEntryDialog(self, "Which letter do you wish to change? Use format: 'a=e'", "Decode Letter", "")
dialogue.ShowModal()
decode = dialogue.GetValue()
#Text entry filter
match = re.search(r'\w+=\w+|^\d*$', decode)
if not match:
err = wx.MessageDialog(self, "That is not a correct entry format.", "Entry Error", style=wx.ICON_HAND)
err.ShowModal()
#Letter replacement
else:
if not MainWindow.quote_altered:
MainWindow.buffer_origin = decode[0].upper()
MainWindow.buffer_replace = decode[2].upper()
else:
for n in range(0, len(MainWindow.split_buffer)):
if MainWindow.split_buffer[n] == MainWindow.buffer_origin:
MainWindow.split_buffer[n] = MainWindow.buffer_replace.lower()
MainWindow.buffer_origin = decode[0].upper()
MainWindow.buffer_replace = decode[2].upper()
origin = decode[0].upper()
replace = decode[2].upper() #For resetting changes one at a time.
for n in range(0, len(MainWindow.split_cryptoquote)):
if MainWindow.split_cryptoquote[n] == origin:
MainWindow.split_cryptoquote[n] = replace.lower()
MainWindow.quote_altered = True
origin = None
replace = None
self.txt.SetLabel("".join(MainWindow.split_cryptoquote))
self.sizer.Layout()
dialogue.Destroy()
def on_generate_quote(self, e):
MainWindow.quote.pop()
MainWindow.quote.append(quote_fetch(quotes))
cryptoquote = generate_cryptogram(MainWindow.quote[0], encrypt_key(shuffle_alphabet()))
MainWindow.split_cryptoquote = []
MainWindow.base_copy = []
for i in cryptoquote:
MainWindow.split_cryptoquote.append(i)
MainWindow.base_copy.append(i)
MainWindow.split_buffer.append(i)
self.txt.SetLabel("".join(MainWindow.split_cryptoquote))
self.txt.SetForegroundColour("BLACK")
self.sizer.Layout()
def on_answer(self, e):
if len(MainWindow.base_copy) == 0:
err = wx.MessageDialog(self, "You haven't generated a puzzle yet, doofus!", "Encryption Error", style=wx.ICON_HAND)
err.ShowModal()
else:
self.txt.SetLabel(MainWindow.quote[0])
self.txt.SetForegroundColour("BLUE")
self.sizer.Layout()
def on_clear_last(self, e):
if MainWindow.quote_altered:
self.txt.SetLabel("".join(MainWindow.split_buffer))
else:
self.txt.SetLabel("".join(MainWindow.base_copy))
self.txt.SetForegroundColour("BLACK")
self.sizer.Layout()
def on_clear_all(self, e):
print MainWindow.base_copy
MainWindow.split_cryptoquote = MainWindow.base_copy
MainWindow.split_buffer = MainWindow.base_copy
MainWindow.quote_altered = False
self.txt.SetLabel("".join(MainWindow.base_copy))
self.txt.SetForegroundColour("BLACK")
self.sizer.Layout()
app = wx.App(False)
frame = MainWindow(None, "Cryptogrammar")
frame.Show()
app.MainLoop()
(I do, however, make a call in on_clear_all to set split_cryptoquote
to base_copy, but this shouldn't change base_copy.)
You've spotted your own problem:
MainWindow.split_cryptoquote = MainWindow.base_copy binds MainWindow.split_cryptoquote to the same object as MainWindow.base_copy, so when you modify one, you modify the other.
If you change the line to
MainWindow.split_cryptoquote = MainWindow.base_copy[:]
You will force python to create a new object (a copy of MainWindow.base_copy) , and this problem should not occur.
Edit: The line below:
MainWindow.split_buffer = MainWindow.base_copy also needs the same treatment, I think.
See this example:
>>> lista = [1,2]
>>> listb = lista
>>> listb.append(3)
>>> lista
[1, 2, 3]
>>> listb
[1, 2, 3]
>>> listc = lista[:]
>>> listc.append(4)
>>> listc
[1, 2, 3, 4]
>>> lista
[1, 2, 3]
I have no idea what is wrong but I keep getting this
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk/Tkinter.py", line 1410, in call
return self.func(*args)
File "/Users/Zane/Desktop/Factorial GUI.py", line 72, in reveal2
self.text2.insert(0.0, message)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk/Tkinter.py", line 2986, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
TclError: wrong # args: should be ".22186144.22187184 insert index chars ?tagList chars tagList ...?"
here is my code:`
from Tkinter import*
class App(Frame):
def fac(self, n):
if n >= 0:
if n == 1 or n == 0:
return 1
else:
return n*self.fac(n-1)
else:
print('Error')
def per(self, n, r):
y = (self.fac(n)) / self.fac(n - r)
print (y)
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.instruction1 = Label(self, text = "Factorial:")
self.instruction1.grid(row = 0, column = 0, columnspan = 1, sticky = W)
self.password1 = Entry(self)
self.password1.grid(row = 0, column = 1, sticky = W)
self.submit_button1 = Button(self, text ="Enter", command = self.reveal1)
self.submit_button1.grid(row = 2, column = 0, sticky = W)
self.text1 = Text(self, width = 30, height = 1, wrap = WORD)
self.text1.grid(row = 3, column = 0, columnspan = 2, sticky = W)
self.instruction2 = Label(self, text = "Permutation:")
self.instruction2.grid(row = 4, column = 0, columnspan = 1, sticky = W)
self.password2 = Entry(self)
self.password2.grid(row = 4, column = 1, sticky = W)
self.password3 = Entry(self)
self.password3.grid(row = 6, column = 1, sticky = W)
self.submit_button2 = Button(self, text ="Enter", command = self.reveal2)
self.submit_button2.grid(row = 7, column = 0, sticky = W)
self.text2 = Text(self, width = 30, height = 1, wrap = WORD)
self.text2.grid(row = 8, column = 0, columnspan = 2, sticky = W)
def reveal1(self):
y = int(self.password1.get())
message = self.fac(y)
self.text1.delete(0.0, END)
self.text1.insert(0.0, message)
def reveal2(self):
y = int(self.password2.get())
z = int(self.password3.get())
message = self.per(y, z)
self.text2.delete(0.0, END)
self.text2.insert(0.0, message)
root = Tk()
root.title('Factorial')
root.geometry("340x300")
app = App(root)
root.mainloop()
`
Almost the only way to get the error you say you get with the code you posted, is if the insert method is called when the data to insert is None. message comes from the result of per, but per returns None because you don't explicitly return anything else.
One of the first things to try when trying to debug is to check that the data you're sending to the failing function is what you think it is. You can do this in a very low-tech way by simply printing out the values being passed to the insert message. This instantly told me that message was None. Once I learned that, it's pretty simple to answer the question "why was it None?".