Hello,
I'm currently building a GUI application in Perl tkx. In the application it is possible for the user to open a subwindow when selecting a certain option. However, the subwindow I create is not modal, that is the user can still operate on the main window. I want to restrict this and make it impossible for the user to operate on the main window until the subwindow has been closed. How do I accomplish this? The code I use to create the subwindow follows here:
my $subwin = $mw->new___toplevel();
$subwin->g_wm_title("Subwindow");
Thank you in advance!
Best regards,
Samuel Alfredsson
Hi Samuel,
You can add following line to disable the main window.
Tkx::wm_attributes($mw, -disabled => 1);
Then you add following line where the command to close the sub window is in your script.
Tkx::wm_attributes($mw, -disabled => 0);
You can also hide and show the main window by following lines.
Tkx::wm_iconify($mw); #To hide
Tkx::wm_deiconify($mw); #To show
I hope this helps.
Shige
Thank you for your answer! The code that you supplied works well. However, I have some further questions.
1. How do I handle the event when the user uses the X-button to close the window instead of the close button that I have put in the window? Of course, I like to enable the main window in this case also.
2. How do I make my subwindow active when I create it? As it is now, the window pops up but the main window is still the active window.
Thank you in advance!
You're welcome. I am glad that I can help you in anyway.
I hope below is going to be the answers to your questions.
1. Good question! Following is the line need to be added in your script after you create the subwindow.
$subwin->g_wm_protocol('WM_DELETE_WINDOW', sub{Tkx::wm_attributes($mw, -disabled => 0); Tkx::wm_deiconify($mw); Tkx::destroy($subwin)});
2. For this one, I am not sure if I understand the issue. Do you mean that the subwindow does not appear on top of main window? Or it appears OK, but is it inactive?
If it is just to make the subwindow appear on top of main. The command is as follows.
Tkx::focus(-force => $subwin);
But when you create the subwindow, you add following two lines. You shouldn't even see main window anymore when the subwindow appears, so again, I am not sure the issue.
Tkx::wm_attributes($mw, -disabled => 1);
Tkx::wm_iconify($mw);
The subwindow should be active when you create it unless there is some problem in the commands. In this case, I need more information.
Thank you.
Shige
Thank you again for your answers, they have helped me a lot.
For the second question, perhaps I was a bit unclear. What I meant was that the window appears OK on top of the main window but it is inactive, insted the main window is still active. This is mostly an estetical issue since as soon as you click on the subwindow it becomes active. But still, I found it annoying. However, your code solved my problem and now it works just as I want it to.
Actually, I have one further question regarding subwindows. My main window does not cover the entire screen and when I create a subwindow I would like it to appear in the same area of the screen as the main window. That is not the case now for all my subwindows. Some of them appear in the other end of the screen which can be a bit confusing. Do you know any good solution to this?
Thank you!
BR,
Samuel Alfredsson
You're welcome, and I am glad to hear that the code solved your problem anyway.
I happen to know the answer to your further question, and I usually use below lines for that purpose. As the quick explanation of the lines, the first two lines get the x and y position values of the main window, and the third line applies the values to the subwindow's position.
$x = Tkx::winfo_x($mw);
$y = Tkx::winfo_y($mw);
Tkx::wm_geometry($subwin, "+$x+$y");
I hope it also works for you.
Thank you.
Shige
Hello again Shige,
As usual I would like to thank you for your help, more and more of the issues on my to do list are solved thanks to you.
I still have one further question (the only one I can come up with at the moment) and it is regarding the icon in the upper left corner of the main window (and all the subwindows too for that matter). I wonder if there is a way to change this icon and set it to an arbitrary picture?
Best regards,
Samuel
Hi Samuel,
You're welcome. I have actually never tried to change the icon, but I just figured out how to do it. The code is as follows.
Tkx::wm_iconbitmap($mw, "icon_name");
The "icon_name" should be replaced with the name of your graphic. The thing is that the graphic file has to be .ico file. Otherwise, I couldn't get it work.
The icon_name should also include the path depend on where the .ico file is located. So the .ico file has to be at common place for all end users of your script. Or for the safe side, you may want to do like below.
(my $path = $0) =~ s/\\[^\\]+$//;
Tkx::wm_iconbitmap($mw, "$path\\icon_name");
Now the icon_name should be just file name. Then you place your .ico file in the same directory as your script. This works no matter where your end users run your script as long as the icon file is in the same directory as your script. By the way, this sample is for Windows OS.
Lastly, the code above only affects to main window. If you want to change the icon on all windows, the code should be as follows.
Tkx::wm_iconbitmap($mw, -default => "$path\\icon_name");
I learned things myself this time.
Thank you.
Shige
Hello Shige,
Thank you for your help, changing the icon worked fine.
Perhaps this is a bit off topic but I'll ask anyway. Now, I'm starting to feel quite pleased with my application and therefore I would like to be able to create a standalone executable that anyone could run without having to install Activeperl. I have tried to find a program that offers that kind of possibility and found that there are several different solutions. Do you know any good program to achieve this? And is there anything important to have in mind while using it?
Thank you in advance!
Best regard
Samuel
Hi Samuel,
You're welcome. I am sorry for the late response.
I have heard of the software called Perl2Exe. This may be something you are looking for. By the way, it is not freeware.
Thank you.
Shige