I am trying to populate 2 columns from my heap list into a text widget on my GUI. I have included my code below which does the same. self.top5 is my list which displays the first 5 elements sorted in descending order
self.tableSize = 25
self.hashTable = [[] for i in range(25)]
self.top5 = [(0,0) * 5]
After creating heap,validating hashkey,appending etc
self.top5 = (heapq.nlargest(5,heap)
self.text.tag_configure('big',foreground = '#5C0000',font('Verdana',9,'bold'))
self.text.tag_configure('sub',foreground = '#000000',font('Verdana',9,'bold'))
self.text.tag_configure('val',foreground = '#000000',font =('Verdana',9))
self.text.insert(INSERT,"\nEMP STATISTICS:",'big')
self.text.insert(END,"\n")
self.text.insert(INSERT,"EMP NUMBER:",'sub')
self.text.insert(END,"\t")
self.text.insert(INSERT,self.eID,'val')
self.text.insert(END,",")
self.text.insert(INSERT,"EMP NAME:",'sub')
self.text.insert(END,"\t")
self.text.insert(INSERT,self.eName,'val')
self.text.insert(END,",")
for k in self.top5:
self.text.insert(INSERT,k[0],'val')
self.text.insert(END, "\t\t")
self.text.insert(INSERT,k[1],'val')
self.text.insert(END,"\t\t\t")`
When i run my program, somehow nothing comes up on my screen. Though there is no error. Also, I am inserting first into my heapq and then fetching the top5 details sorted using nlargest.
Can someone please explain why am I not able to see any data on my GUI? When I use print statements at certain checkpoints, it works and I can definitely see proper output on terminal. I am a newbie, pardon me if it's too silly/obvious.
I'm pretty sure in order to display a GUI you need:
root = tkinter.Tk()
root.title("YOUR TITLE HERE")
And then you need to use pack() after whatever you want to be displayed on the GUI and once that's done you need root.mainloop() to display the GUI altogether.
Related
First sorry if I am missing anything.and sorry about the format, new to stackoverflow
I am trying to read the flag state from a QtreeWidget.
I have tried a lot of things and nothing seam to work.
I am dynamical creating the tree as follows
Part=str(result2[row2][0])
qty=str(offset_B)+str(result2[row2][1])
instock=str(offset_B)+str(result2[row2][2])
B = QTreeWidgetItem(A, [Part,qty,instock])
B.setForeground(0,QtGui.QBrush(QtGui.QColor(color_item)))
B.setForeground(1,QtGui.QBrush(QtGui.QColor(color_item)))
B.setForeground(2,QtGui.QBrush(QtGui.QColor(color_item)))
B.setFlags(Qt.ItemIsTristate)
B.setCheckState(0, Qt.Unchecked)
I have a button Generate Po's and when I click it I want to iterate through the items and grab the text value of the checked Items.
sample
so it should return a list of some sort with all the text field.
I can do the list I just need to understand how to grab the "State" of the button.
I hope this is clear enough.
So basically I am reading a valuefrom a text file which is displayed on the Profile frame using a label.
def view_value(self):
self.user = self.controller.user
self.view_value()
with open(self.user + '.txt', "r") as f:
value_line = 2
for i, line in enumerate(f):
if i == value_line:
self.value.set(line)
self.value.config(textvariable=line)
When I go to a different frame to calculate this value again, it will update the text file with the newly calculated value. However, when I go back to the previous page using the back button - the old value is still there. To get the new value to appear I need to reopen/re-run the program.
Is it possible to have the newly updated value displayed on the page without restarting the application? I have tried calling my view_value method to try and update the value and also tried configuring the label from the other class but wasn't able to get it working.
I also realise there are probably a million things wrong with my code, I am very new to Python so apologies!
You need to update the StringVar Profile.allowance in order to make the display in Profile page updated. The simple way is adding the following statement before self.controller.show_frame(Profile) in write_to_file() of CalculateAllowance class:
self.controller.frames[Profile].allowance.set(self.user_data[2])
Also you need to fix the following issues in your code:
Remove calling self.view_allowance() inside view_allowance() in Profile class as it will cause infinite recursion problem.
Remove self.holiday_allowance_amount.config(textvariable=line) in view_allowance() as it wrongly reassigns textvariable to a string.
I have a situation in which i create a few checkboxes and a 'save' button on a frame. Clicking on the button will save the current 'check status' of the checkboxes and close the frame (the frame has another frame as it's parent). The 'check status' is stored in a list in a separate .py file.
There's another button, clicking on which would i would want to restore those same check values on the check boxes. I am not sure what approach is proper here: whether to save the checkbox objects itself and load them or to create new checkboxes every time and apply the check status values on them.
This issue has taken up quite some time of mine as i am new to the python GUI concept, any help would be much appreciated.
P.S: if any details are required to make the question more clear kindly let me know.
EDIT: here's a small representation of what i have tried:
#file that generates the checkboxes (part of a bigger code)
var_list=[] #list of the variables of the checkbox objects
load_status = savedata.load() #fetch the saved status list from another file, if it exists
if len(load_status)!=0: #if the checkboxes have already been created before
for count in range(len(var_list)):
if load_status[count] == 0:
var_list[count].set(0) #set/apply the values from the status list
else:
var_list[count].set(1)
return
def check(var_list):
check_status_list=[]
for i in range(len(var_list)):
c = var_list[i].get()
check_status_list.append(c) #the status of the checkboxes that is stored in another file
var = IntVar()
var_list.append(var)
poll_check = ttk.Checkbutton(parent,variable=rr,command=lambda:check(var_list))
poll_check.place(x=x1,y=y1)
x1=x1+15
when i click on the save button the status list (check_status_list) is stored in another file. On clicking the third button ('load') i fetch the status list first and (try to) set the values on the already exsting checkbox objects.
Kindly feel free to point out the (obviously) silly mistake that this approach has.
I am making a stock control program and i have hit a problem with getting the value of a selected cell, i know i need to use "QtGui.QTableWidget.currentRow" and "QtGui.QTableWidget.currentColumn" to get the item's position. However i cannot seem to get this to work because when the functions are called nothing has been selected and so it returns -1,-1
Does anyone know how to get it so it runs the "QtGui.QTableWidget.currentRow" and "QtGui.QTableWidget.currentColumn" everytime the user selects a cell?
i think the code i need to get the actual data once i have the co-ords is QtGui.QTableWidget.item ?
This is the code i am using to get the row and column:
row = self.table.currentRow
column = self.table.currentColumn
self.ID = self.table.item(row, column)
when the user clicks a button to add stock the program should then use the product code it will have just got to make the change to the database after getting the quantity added from the user
I am using python 3.2 and pyqt 4
any help would be appreciated
Thank you
Sam
When the QTableWidget sees that someone has clicked one of it's cells, it will emit a cellClicked event - which you need to connect to. Maybe something like
self.table.cellClicked.connect(self.cell_was_clicked)
could be in your setup code,
and the function cell_was_clicked might be something like
def cell_was_clicked(self, row, column):
print("Row %d and Column %d was clicked" % (row, column))
item = self.table.itemAt(row, column)
self.ID = item.text()
I've not used currentRow and currentColumn as you want a response on the click. This function is documented here (sorry, I prefer pyside - it's pretty much the same as PyQT). Note also itemAt instead of just item - as you will get the item at the cell, not it's contents. Use the .text() function of QTableWidgetItem to get at the contents.
Note - this is using 'slots and signals' and in particular the 'new style'. As you're using PyQT4 and Python 3 you should have no trouble with 'new stuff' :-)
You might consider browsing a slots and signals tutorial - that might straighten a few of these abstract concepts out. Good luck!
here is the code that worked for me:
def get_selected_cell_value():
current_row = self.sold_items_details_table.currentRow()
current_column = self.sold_items_details_table.currentColumn()
cell_value = table.item(current_row, current_column).text()
I have a Ttk Notebook widget containing 8 Frames - so, 8 tabs. Each frame contains a Text widget. I have a button outside the Notebook widget, and I want to insert text into the current tabs Text widget when this button is pressed.
This would seem to require working out which widget in the Notebook is currently selected, but I can't seem to find how to do this. How would I find the currently selected tab?
Alternatively, how can I implement what I want to?
If it helps, here's the code for my notebook:
self.nb = Notebook(master)
self.nb.pack(fill='both', expand='yes', padx=10, pady=10)
self.frames = []
self.texts = []
for i in xrange(8):
self.frames.append(Frame())
self.nb.add(self.frames[i])
self.texts.append(Text(self.frames[i]))
self.texts[i].pack(fill='both')
You can retrieve the selected tab through select method. However, this method returns a tab_id which is not much useful as is. index convert it to the number of the selected tab.
>>> nb.select()
'.4299842480.4300630784'
>>> nb.index(nb.select())
2
Note that you coud also get more information about the selected tab using tab
>>> nb.tab(nb.select(), "text")
'mytab2'
You might look at Notebook reference documentation : http://docs.python.org/3/library/tkinter.ttk.html#notebook
You can get currently selected tab using the "current" keyword:
noteBook.index("current")
Check this website:
https://docs.python.org/2/library/ttk.html#tab-identifiers
24.2.5.3. Tab Identifiers
There are two simple ways to see which tab is selected:
nb.select() # returns the Tab NAME (string) of the current selection
and
nb.index('current') # returns the Tab INDEX (number) of the current selection
The .select() method can also be used to select which tab is currently active, via nb.select(tabId). Without the arg, it returns the tabId (in "name" form) of the current selection.
The .index(tabId) converts a tabId into a numerical index. It also can take the string "end" which will return the number of tabs. So, nb.index(tkinter.END) is like a len() method for a notebook widget.
When there are no tabs, .select() returns an empty string, but .index('current') throws an exception. So, if you want the index, I would say
if nb.select():
idx = nb.index('current')
is the best way to go.
In your particular case, you would probably want to grab the current notebook tab name and then convert that name into the actual child text widget, via the nametowidget() method, for manipulation. So...
tabName = notebook.select()
if tabName:
textWidget = notebook.nametowidget(tabName) # here, 'notebook' could be any widget
textWidget.insert(pos, text, tags)
The nametowidget(name) method maps a Tkinter name to the actual widget. It is a method callable by any actual widget.
I am not a expert at all but hope i can help with some "fresh eyes".
I imagine it could be something involving
def buttonclick():
somevariablename = focus_get()
#Print your text into the somevariable notebook could be
#something like(not sure about the syntax):
focusednotebook = somevariablename
focusednotebook.insert('1.0', 'your text here')
yourbutton = Button(parent, text = "button name", command = buttonclick)
yourbutton.pack()
Hope it works or get you in the right direction.
Please feel free to edit as I am fairly new here amd with python :-)
Getting the tageted tab in tk.Notebook it's easy all you have to do is to use the notebook object and target the index of the current tab. This can be done as follows
# creating a notebook object
notebook = ttk.Notebook(root, height=height, width=width, padding=20)
# Adding tabs
notebook.add(bin_tab, text="Binary Conversion")
notebook.add(oct_tab, text="Octal Conversion")
notebook.add(hex_tab, text="Hexadecimal Conversion")
print(notebook.index("current")) # returns 0, 1, 2depending on how many tabs you have in my case i have 3 which means index from 0 to 2