clearing text box elements in perl tk - perltk

I am building a GUI in Perl tk and I have made a reset button which is supposed to clear elements of a text box:
use Tk;
use Tk::Text ;
............
sub reset9 {
$txt2-> delete('1.0','end');
}
This is giving an error saying it cannot call method Delete on an undefined value. I have checked for all syntax errors and I am using Strawberry Perl for Windows.

Your subroutine does not know what $txt2 is. If $txt2 was defined inside another subroutine add the word "our" before it (instead of using my).
i.e.
our $txt2 = ...
That should do the trick for you

You may try :
$txt2-> delete('0.0','end');

Related

Can I format variables like strings in python?

I want to use printing command bellow in many places of my script. But I need to keep replacing "Survived" with some other string.
print(df.Survived.value_counts())
Can I automate the process by formating variable the same way as string? So if I want to replace "Survived" with "different" can I use something like:
var = 'different'
text = 'df.{}.value_counts()'.format(var)
print(text)
unfortunately this prints out "df.different.value_counts()" as as a string, while I need to print the value of df.different.value_counts()
I'm pretty sure alot of IDEs, have this option that is called refactoring, and it allows you to change a similar line of code/string on every line of code to what you need it to be.
I'm aware of VSCode's way of refactoring, is by selecting a part of the code and right click to select the option called change all occurances. This will replace the exact code on every line if it exists.
But if you want to do what you proposed, then eval('df.{}.value_counts()'.format(var)) is an option, but this is very unsecured and dangerous, so a more safer approach would be importing the ast module and using it's literal_eval function which is safer. ast.literal_eval('df.{}.value_counts()'.format(var)).
if ast.literal_eval() doesn't work then try this final solution that works.
def cat():
return 1
text = locals()['df.{}.value_counts'.format(var)]()
Found the way: print(df[var].value_counts())

Is there a better way to handle double click in pygobject

I've been using the following for handling double click:
def do_button_press_event(self, eb: Gdk.EventButton):
if eb.type == Gdk.EventType._2BUTTON_PRESS:
# todo: code double click
pass
Accessing the private property _2BUTTON_PRESS feels a bit dirty. Is there a better way to handle this?
It's not a private property: it's an artefact of the C enumeration member being GDK_2BUTTON_PRESS. Python does not allow identifiers to start with a number, so when translating the symbol GDK_2BUTTON_PRESS in the GdkEventType C enumeration into a field in Gdk.EventType Python class, PyGOBject needs to escape the 2BUTTON_PRESS part.
To avoid this, GTK introduced a GDK_DOUBLE_BUTTON_PRESS, which is correctly translated as Gdk.EventType.DOUBLE_BUTTON_PRESS.
The same explanation also applies to the GDK_3BUTTON_PRESS/GDK_TRIPLE_BUTTON_PRESS enumeration fields.

Trying to add items into a ComboBox via MFC

I am trying to add items to a ComboBox so the user has a choice of what constant to run a calculation with but I cant seem to add items to a ComboBox without an error.
CComboBox *m_YM = (CComboBox *)GetDlgItem(IDC_COMBO1);
I have tried:
m_YM->AddString("Wood");
m_YM->Items->Add("Wood");
m_YM.InsertString(0, "Wood");
All throw errors. Compiler tells me that:
The argument type is incompatible with LPCTSTR.
No idea what is the meaning.
The important thing is the middle T of LPCTSTR, which means it will automatically decide if your string is Unicode or plain old ASCII, but the string needs to be input properly.
Recommended reading: What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
Try to put an L before or enclosing inside _T(...). Example: L"Wood" or _T("Wood")

How do I print colors into the console with D?

I have tried escape sequences with the writeln() function, I also tried to use them with the printf() function imported from the std.c.stdlib module, but it only prints an empty line.
printf("\0x1B[5;32;40m Blink Text");
printf("\e[5;32;40m Blink Text\e[m");
writeln("\0x1b\x5b1;31;40m\tColor");
None of these work.
I have tried everything I can think of, is there a way?
Searching the D website's library reference didn't help me.
EDIT: SOLUTION
Okay, so I have tried to import the function SetConsoleTextAttribute, as Mars kindly suggested:
extern (Windows) bool SetConsoleTextAttribute(void*, ushort);
I also imported the other function (Which I simply guessed I need to import, as I have no previous experience with Win programming)
extern (Windows) void* GetStdHandle(uint);
And simply called the two functions
auto handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_BLUE);
writeln("In Color");
This works perfectly, thank you all so much for your time and help
Like CyberShadow pointed out, you have to use \x1B, or \033. It should work fine, as long as you're on Linux. Windows doesn't support those codes though. Here you have to use the API function SetConsoleTextAttribute from std.c.windows.windows.
There is a typo in your string: use \x1B instead of \0x1B.
D doesn't support the \e escape code in strings, use \x1B.
You may also try a helper module like http://www.digitalmars.com/d/archives/digitalmars/D/Color_your_terminal_s_output_146182.html

XDebug and VIM. Browsing array values

Is there any way to browse the data inside an array?
Currently I can only see $data[0] = (array) with no way knowing what's inside the array.
I can the values inside normal variables fine.
Is there a way to see inside the arrays? Maybe a command I'm not aware of?
Edit:
I found out I can use the command keys ,e to evaluate an array or object.
After I type ,e an /*{{{1*/ => eval: prompt shows up then I can type /*{{{1*/ => eval: $data[0] to see the values.
Except I get it in the following output format:
/*{{{1*/ => eval: $data[0]
$command = 'eval';
EVAL_RESULT = (array) ;
EVAL_RESULT = (string) 'stringfromdata0-1' ;
EVAL_RESULT = (string) 'stringfromdata0-2' ;
EVAL_RESULT = (array) 'stringfromdata0-3' ;
This only does half of what I want it to do. Is there any way to output the keys of the array? It only shows me the values, but the keys are shown as "EVAL_RESULT" instead of the their perspective key names of the array.
Edit debugger.vim file (~/.vim/plugin/debugger.vim) and find a line similar to
let g:debuggerMaxDepth = 1
increase the depth varibale to a reasonable amount (5 should be enough)
save and restart vim.
Also, you can wrap your expression in var_export(<expr>, true), and it will show you the full object.
You can enter the vim command ,e in a xdebug session.
From there you can evaluate any php line you want; for example
print_r($data);
and submit it with Enter
Note: This will output to your php-cli stdout, or possibly output buffer if you are inside a ob_start block; Or if you are accessing from a browser, it may not output until the entire php request is completed. You may be able to flush a partial output buffer to a browser, but you'll have to Google that one for yourself.
I Posted this as an answer even though its posted in the OP's question because I don't read the OP's question if I'm looking for an answer and I want to make sure people can find it! If the OP posts his answer as an answer and ping's me I'd be happy to delete this one.
Never got it the way I wanted it to work. Instead I found something way better.
Using Vundle I installed the VIM debugger for xdebug below:
https://github.com/joonty/vdebug
I'll post a screenshot whenever I get a chance.
Works like a charm though.

Resources