python tkinter radiobutton state - python-2.7

How do you get the state of a Button (clicked or unclicked)? This is a button state and not a question about variables set by the button. How do I tell if an object in a Frame is selected using i.winfo_class() to identify a Radiobutton.
i.e.
for i in a.winfo_children():
if i.winfo_class() == "Radiobutton":
i.get()
Radiobuttons have no get attribute so this will never work, but the logic is the same. I've looked through documentation without success.

The only solution is to get the value of the associated variable and compare it to the value of the radiobutton. If they are the same, that radiobutton is selected.

Found the solution. ttk.Radiobutton has more functionality than the tkinter.Radiobutton.
Please make note there there are major issues with all methods and values dereferences by Radiobuttons even now. Sometimes you get a pointer, sometimes you get a string and need to dereference yourself. This makes Radiobuttons a more difficult Widget to deal with than most of the others I've been working with. There are many documents on this and they are fixing this, but it is something to note.
For my purposes, tkinter.Radiobutton has no instate which means there is no simple way to check if it is clicked. I was getting pointer after pointer and dereferencing was far too tedious and complex to code for a dynamic gui.
The code I've ended up with is as follows:
for i in a.winfo_children():
if i.winfo_class() == "TRadiobutton" and i.instate(['selected']) is True:
return i.cget('value')
Also make note that TRadioButton string is used instead of RadioButton, the object type returned is not the same between ttk and tkinter implementations of Radiobutton.

Related

Is there a way to get scrollstate from Lazyrow

I have made a LazyRow that i now want to be able to get the scrollposition from. What i understand Scrollablerow has been deprecated. (correct me if im wrong) The thing is that i cant make a scrollablerow so i thought lets make a lazy one then. but i have no clue how to get scrollposition from the lazyrow. i know how to get index but not position if that eaven exists. here is what i have tried.
val scrollState = rememberScrollState()
LazyRow(scrollState = scrollstate){
}
For LazyScrollers, there are separate LazyStates.
I think there's just one, in fact, i.e. rememberLazyListState()
Pass that as the scroll state to the row and then you can access all kinds of info. For example, you could get the index of the first visible item, as well as its offset. There are direct properties for this stuff in the object returned by the above initialisation. You can also perform some more complex operations using the lazyListState.layoutInfo property that you get.
Also, ScrollableRow may be deprecated as a #Composable, but it is just refactored, a bit. Now, you can use the horozontalScroll() and verticalScroll() Modifiers, both of which accept a scrollState parameter, which expects the same object as the one you've created in the question.
Usually, you'd use LazyScrollers since they're not tough to implement and also are super-performant, but the general idea is that they are used with large datasets whereas non-lazy scrollers are okay for small sized lists and stuff. This is because the lazy ones cache only a small fraction of the entire list, making your UI peformant, which is not something regular scrollers do, and not a problem for small datasets.
They're like equivalents of RecyclerView from the View System

Django - can I sotre and run code from database?

I build program, when in one part I have some ranking, and I would like to give users option to customize it.
In my code I have a function that gets objects and returnes them packed with points and position in ranking (for now it calculates the arithmetic mean of some object's values).
My question is is it possible to give e.g. admin chance to write this function via admin panel and use it, so if he would like to one day use harmonic mean he could without changing source code?
Yes, you could just store a string in the database and exec() it with suitable arguments...
However, you'll have to be careful – Python code can practically never be sandboxed perfectly. In the event that you accept any arbitrary Python code for this, and someone with nefarious intents gets to your admin panel to change the expression, they can do practically anything.
In other words, don't use raw Python for the code you store.

How to make sure that a checkbox appears unchecked in Tkinter

I have a problem which looks like some kind of a bug. Sometimes it happens that a Checkbutton first appears checked when created and it seems like there is no apparent reason for this behaviour. I'm using external variables to get information about status of the checkbox and I'm sure I reset them right before the declaration. My declaration looks like this:
#item is a string
cbtn = Checkbutton(master, command=lambda method=item: fun(method))
You can see there's even no variable or state argument in the declaration, so I think this should always create an empty checkbox. Yet somehow, sometimes it appears checked at first, which is a problem because I'm creating a lock and then it looks opposite than it's supposed to.
So, is there a sureproof way to make it appear without a checkmark?
So, is there a sureproof way to make it appear without a checkmark?
Yes. Explicitly set the onvalue and offvalue attributes, associate a variable with the checkbutton, and explicitly set the value of the variable to the same as the offvalue.

Qt - Set global precision for all tables and models in a program

I will preface this question by mentioning I'm honestly not even sure what I'm trying to do is possible. I've worked with Qt for a while now and never tried to do anything like this, and I've found next to nothing online about how to accomplish my goal, so it may well not be something that Qt can do. I figured it still couldn't hurt to ask in case I'm overlooking something.
I am working on a very large project (~200 files/classes in all) in Qt Creator 3.0.1 (based on Qt 5.2.1). Within this program we have a lot of tables that display numeric data, using probably a dozen or so different model classes fr different tables.
Today my project lead requested that a feature be added to our settings (which we store using the QSettings class) to allow our users to set the decimal precision of any numeric data that is displayed in a table anywhere in the program. I know how to do this using std::setPrecision() and calling QSettings::value().toInt() to get the stored precision. However, it seems like that would have to be done explicitly every time a value is returned by a Model class's data() function, which would literally mean adding that function call hundreds of different places in our code. This seems extremely bulky and tedious, and also very prone to causing bugs, since with so many places in the code that would need it, it would be very easy to accidentally overlook places where it should be used. Also, this would be rather tedious in the future if we end up adding new dialogs with table displays and new models, since those new ones would also need that function called every time a number is needed.
It seems like a very simple, efficient, and elegant way to solve this issue would be if Qt had a way to store a global precision for not just one dialog or one model class, but for the entire project. That way, instead of needing to use setPrecision() every time a number is displayed in a dialog, the program would already know how many places after the decimal to show. It seems like using this as some kind of meta-variable for the program as a whole would save tons of time and effort changing everything over now and adding things in the future. However like I said above, I do not know if it is even possible in Qt to specify for a whole program something like precision in display dialogs. If anyone has any ideas how to do something like this, or any other way to specify precision in models without needing to refer to precision each time a value is added to a display, I'd really appreciate any help. Thanks!
Your models should not care about the format of displayed data. What you can do is to inherit from QStyledItemDelegate and override displayText method.
The method can look something like:
QString BaseItemDelegate::displayText(const QVariant &value,
const QLocale &locale) const
{
if (value.type() == QVariant::Double) {
//here you can retrieve precision of displayed double
//from some settings object
return QLocale().toString(value.toDouble(), 'f', 2);
}
return QStyledItemDelegate::displayText(value, locale);
}
Then you can attach this delegate to your views (QAbstractItemView::setItemDelegate()). Of course you must make your models to return double instead of preformatted strings.

Putting Controls at ToolBar- Stuck

I have been staring at this documentation for 5 hours now. I simply cant connect the steps. If you guys can enlighten me of the stuff.
Here is the site:
http://msdn.microsoft.com/EN-US/library/bb983718(VS.110).aspx
So my problem are the following:
-at number 5, it asked me to "Set these parameters as follows:", it didnt even mention anything about where? Where to implement the constructor, and why are we using CMFCToolbarComboBoxButton? when it already asked me at step 4 to derive a clas called CFindComboButton. Shouldnt I be making a contstructor for that one instead?
-at number 4(sorry about the non organized numbering of problems), what I did is use the add class (not the class wizard), and then I picked MFC Class. I then enter the supposedly CFindComboButton and Base class as CMFCToolBarComboBoxButton. Did I do something wrong on this one? Do I have to do anything for the ID ID_EDIT_FIND_COMBO?
-When I register the ID_EDIT_FIND_COMBO at the String Table, I dont exactly know what I did. Did I just register an id for future implementation? or is it something else?
-So I cant do step 5, I skipped to step 6. All it ask me is to look for the CreateCombo method athe the override section of properties at CFindComboButton. Well I can only find 3 override. None of them are CreateCombo method. Well from there, you can tell that I'm lost.
I'm a noob at mfc so you might wanna take that in consideration.
Even though your question is a bit jump-led up, let me try and answer so that it works for you.
Create two class - one derived from CComboBox (call it CFindComboBox) and another from CMFCToolBarComboBoxButton (call it CFindComboBoxButton). First class will implement the Combobox that will be shown when you click the drop down button in the toolbar. This drop down button is implemented by CFindComboBoxButton. Hope this is clear.
Now define the constructor for the CFindComboBoxButton as CFindComboBoxButton(UNIT nID, int nImage, DWORD dwStyles) using three parameters as explained below:
Command ID of the button which will be ID_EDIT_FIND_COMBO (or anything you want to define it as). This will get defined in the String Table. Just add a new entry in String Table with ID_EDIT_FIND_COMBO as ID and a placeholder string. Don't omit the string value else the ID will not get defined. The string value can be anything as it wont be used anywhere.
Second parameter will just be a call to CCommandManager::GetCmdImage(ID_EDIT_FIND). This will return the default image used to show the drop down for combobox. In case you want to use your own custom image you can create one and instead pass the ID of that.
Third parameter is the styles you want to use. They are defined at http://msdn.microsoft.com/EN-US/library/7h63bxbe(v=vs.110).aspx but you can use the default value (CBS_DROPDOWNLIST) to start with.
Override the CreateCombo method of CMFCToolBarComboBoxButton and add its implementation to CFindComboBoxButton. In this method create and return a pointer to CFindComboBox (CComboBox derived class).
I hope this clears all the confusion and you should be on your way to have a custom Combobox embedded inside a toolbar.
take a look at the VisualStudioDemo Sample:
http://msdn.microsoft.com/en-us/library/bb983983%28v=vs.90%29.aspx
you can find the CFindComboButton implementation there