CheckBox column in a JTable - python

Does anyone know how to add checkboxes to JTable column in Python?
I need to add a checkbox column to my table. I have already created a table using swing library: DefaultTableModel.
Thanks in advance.

Unfortunately there is no solution in this regard. I tried to use some java methods but with no results.
For example, I used Boolean(0) and Boolean(1) but both are recognized as True False values to be inserted in the table.
To select the lines I suggest you to use ctrl + click (Windows) or cmd + click (MacOS) and then use this piece of code:
i = 0
rowsToPrune = table.getSelectedRows()
while i < len(rowsToPrune):
tableModel.removeRow(rowsToPrune[i] - i)
i += 1
I often used this piece of code and it always went well, even for users!

Related

How to enter character(Captial Letter) and number combination using GUI automation modules?

I am doing an automation project, currently my aim is to type certain value in a data entry area of a website in an VDI environment.
The idea is to take a value from a dictionary and paste it into a website in an allocated area.
I cannot able to use selenium and pyautogui is not in this case working properly.
I use pynput key function for the entries so far.
Now the problem is, I need to enter a string like 'T123'.
I use keyboard.type('T123'), then it will only print like 123.
Shift function make all other terms different.
Either I am looking for a code which will type like 'T123' or is it possible to press Shift only for one time to enter 'T', for remaining value enter as normal.
Could someone help me with Pynput module or any other modules useful in this case?
I will show you the code below
import pyautogui
import pandas as pd
from pynput.keyboard import Controller, Key
keyb = Controller()
# click on the location to add the entry.
button = pyautogui.locateCenterOnScreen("image.png", grayscale=True, confidence=0.8)
pyautogui.doubleClick(button)
# read the excel file
df = pd.read_excel('Productlist.xlsx', sheet_name = 'Table3',na_values='n/a')
# convert into dictionaries
b = df.to_dict()
#get the data of references column from the dictionaries
b['References'].values()
#type this reference from a column ie., 'T123' to the website in VDI environment
keyb.type(b['References'][0])
When I run the code, I am only getting the result as 123, capital letter T is missing. But if I print in Python, it will be 'T123'.
I am looking for some code print this Capitalletter and numbers together.
OR
Press shift one time and enter the first digit. then release the shift and enter the remaining digits. Some code supporting it too, will be really helpful.
Any leads appreciated. Thanks

Changing same word in code at the same time with google colab

I'm using colab to work on python projects and was wondering if there was a way to highlight a certain text and change that text for all of the other places in the code.
Example:
variable = 1
variable2 = variable + 1
And then change variable to var without having to individually change every single line
var = 1
var2 = var + 1
Google colab uses the same keys short-cuts of microsoft VS, just use Ctrl+D to add to the selection the next occurrence.
Use instead Ctrl+Shift+L to select all occurrence.
Once you selected the occurrences you can edit them by just typing the new name.
If you use short variable names ( as X) this method will probably not work, as every word with an x in it will also change.
You could use Find and Replace on the Edit tab. Another useful trick is clicking in every place you want to edit while pressing alt and you have multiple text cursors editing at the same time.

Using pywinauto to read a value within a window and use in a function

I am building an automation script with python using pywinauto, and I am stumped on this part. So far I'm able to start the app, or connect to it. I've toggled through the windows in the interface, and now it needs to somehow read the value and compare to the contents of a .csv file and continue with more functions. The app has multiple windows and one of them uses a spreadsheet type interface. There is not a find/replace feature, or else I could just use that.
Now comes the challenge. It needs to "see" the string within a cell and I can easily do this using AccExplorer or Inspect.exe.
This is the cell structure in the app, with the selected item "CAM 2".
Cell example from the app
AccExplorer window showing results of the cell
And this is the result provided by AccExplorer. The red circle indicates the "value" I wish to find, and use for comparison. (I have found that searching for this topic on "value" results in answers that are too vague, rather than my literal need to find the value of "value" in this case.)
By using this code in my script passing in the class of the window provided by the AccExpolorer tool (red arrow for the class)
edit = wdow['WindowsForms10.Window.8.app.0.378734a']
props = edit.GetProperties()
print(props)
It does not return the "Value" field or its property in this case should be "Cam 2"
{'class_name': 'WindowsForms10.Window.8.app.0.378734a',
'friendly_class_name': 'WindowsForms10.Window.8.app.0.378734a',
'texts': [''], 'control_id': 1639674, 'rectangle': <RECT L0, T47, R1366, B746>,
'is_visible': True, 'is_enabled': True, 'control_count': 76, 'style': 1442906112,
'exstyle': 65536, 'user_data': 0, 'context_help_id': 0,
'fonts': [<LOGFONTW 'MS Shell Dlg' -11>],
'client_rects': [<RECT L0, T0, R1366, B699>],
'is_unicode': True, 'menu_items': []}
I am rather new to python (and programming in general) but I have been doing quite well at grasping all of this. I am aware of the backend, which I have not had luck with using UIA, and it seems to be working so far with the default. Also, I've tried using SWAPY and it displays a lot of class names as duplicates, and does not show this level of cell values directly.
The main question is what would I be doing wrong to get this data, or is it even possible to get it this way? I am open to any suggestions or even using other libraries. I just figured this would be the cleanest and easiest. Thanks!
For begin read getting started guide and see some examples
You choose not better way to get cell of DataGridView, try to use UIA backend for that
from pywinauto import Desktop
dlg = Desktop(backend="uia")["YourApplicationName"]
# use that function for found your DataGridView
#dlg.print_control_identifiers()
datagrid = dlg.DataGridView
# use for found some particular cell
#datagrid.print_control_identifiers()
# in my case it was DataItem
cell = dlg.DataGridView.DataItem
# way to get item value
print(cell.legacy_properties()['Value'])
You also can use indexes for choose from a lot of same cells, like "DataItem0" or get all cells using lambda:
cells = [child for child in datagrid.descendants() if child.element_info.control_type == "DataItem"]
for cell in cells:
print(cell.legacy_properties()['Value'])

how to assign a word table in the center of the page

center a word table
i am working with python and word application
i am trying to generate a table which will be in the middle of the page.
i tried something like this :
oWord.Selection.ParagraphFormat.Alignment =
Word.WdParagraphAlignment.wdAlignParagraphCenter;
i had tried to assign it to 1 but it does not matter
oWord.Selection.ParagraphFormat.Alignment = 1
thank you for any help
oWord.Tables[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
// Where Tables[1] means first table into your document. It Would definitely work
Try recording a macro where you execute the ops you want and looking at resulting VB, this may then be easy to translate to python.

Retrieving cell data from a selected cell in a tablewidget

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()

Categories