Importing an image into Tkinter [duplicate] - user-interface

Consider below example:
import tkinter as tk
root = tk.Tk()
root.title("root")
other_window = tk.Tk()
other_window.title("other_window")
root.mainloop()
and also see below example that creates instances of Tk back-to-back instead of at once, so there's exactly one instance of Tk at any given time:
import tkinter as tk
def create_window(window_to_be_closed=None):
if window_to_be_closed:
window_to_be_closed.destroy()
window = tk.Tk()
tk.Button(window, text="Quit", command=lambda arg=window : create_window(arg)).pack()
window.mainloop()
create_window()
Why is it considered bad to have multiple instances of Tk?
Is the second snippet considered a bit better, or does it suffer from
the same conditions the first code does?

Why is it considered bad to have multiple instances of Tk?
Tkinter is just a python wrapper around an embedded Tcl interpreter that imports the Tk library. When you create a root window, you create an instance of a Tcl interpreter.
Each Tcl interpreter is an isolated sandbox. An object in one sandbox cannot interact with objects in another. The most common manifestation of that is that a StringVar created in one interpreter is not visible in another. The same goes for widgets -- you can't create widgets in one interpreter that has as a parent widget in another interpreter. Images are a third case: images created in one cannot be used in another.
From a technical standpoint, there's no reason why you can't have two instances of Tk at the same time. The recommendation against it is because there's rarely an actual need to have two or more distinct Tcl interpreters, and it causes problems that are hard for beginners to grasp.
Is the second snippet considered a bit better, or does it suffer from the same conditions the first code does?
It's impossible to say whether the second example in the question is better or not without knowing what you're trying to achieve. It probably is not any better since, again, there's rarely ever a time when you actually need two instances.
The best solution 99.9% of the time is to create exactly one instance of Tk that you use for the life of your program. If you need a second or subsequent window, create instances of Toplevel. Quite simply, that is how tkinter and the underlying Tcl/Tk interpreter was designed to be used.

I disagree with the tkinter community discouraging the use of multiple tk.Tk windows. You can have multiple tk.Tk windows. Using multiple instances of tk.Tk is the only way to create windows that are truly independent of each other. The only mistake most people make when creating multiple tk.Tk windows is that they forget to pass in master=... when creating PhotoImages/StringVars/IntVars/...
For example look at this code:
import tkinter as tk
root = tk.Tk()
root2 = tk.Tk()
variable = tk.StringVar() # Add `master=root2` to fix the problem
entry = tk.Entry(root2, textvariable=variable)
entry.bind("<Return>", lambda e: print(repr(variable.get())))
entry.pack()
root.mainloop()
The code above doesn't work. If you add master=root2 to the tk.StringVar(), then it will work perfectly fine. This is because tkinter stores the first instance of tk.Tk() in tk._default_root. Then if you don't pass in master=..., it will assume that you wanted the window in tk._default_root.
Another thing people get wrong is how many times should .mainloop() be called. It handles events from all tk.Tk windows that are alive so you only need one .mainloop().
For folks who disagree, I'd be interested in an example of where an actual problem is caused by the multiple tk.Tk windows.

The best reference I've found so far is the Application Windows section of the tkinterbook:
In the simple examples we’ve used this far, there’s only one window on the screen; the root window. This is automatically created when you call the Tk constructor
and
If you need to create additional windows, you can use the Toplevel widget. It simply creates a new window on the screen, a window that looks and behaves pretty much like the original root window
My take on it is that a Tk instance creates a Toplevel widget, plus things like the mainloop, of which there should be only one.

Tk() initializes the hidden tcl interpreter so that the code can run, as Tkinter is just a wrapper around tcl/tk. It also automatically creates a new window. Toplevel() just creates a new window, and wont work if Tk() hasn't been instantiated, as it requires the tcl interpreter that Tk() initializes. You cannot create any Tkinter widgets without instantiating Tk(), and Toplevel is merely a widget. In the question, you use Tk() to create a second window. You should instead create another file, because initializing the tcl interpreter multiple times can get confusing, as #Bryan Oakley explains so well. Then you should do:
from os import startfile
startfile(nameOfTheOtherFile)
, because, as Toplevel() is just a widget, it closes when the Tk() window is closed. Having the other window in a separate file makes it less confusing.

Related

Why isn't my localscript working?

I am basically trying to script a GUI control panel that allows users to accelerate the train with a power lever (in the GUI) and a reverser (Forward, Neutral, Reverse)
I have made two scripts. One is a localscript, the other being a regular script. The regular script enables the GUI on a player's screen when the player sits in the seat. The localscript is what makes the GUI function.
I've scripted the entire thing but only the regular script seems to function, The localscript does not seem to function as it is scripted to. The GUI appears on the screen but when pressed it does nothing.
Here are the contents of the localscript
http://pastebin.com/raw/XnT2Bi2X
Here's are all the objects in the Workspace that is mentioned and relevant to the script:
https://s31.postimg.org/4hd2up2ij/Screen_Shot_2016_06_24_at_11_15_21_AM.png
What are the errors in the localscript that is not allowing the GUI to function as its scripted to?
(I apologize for the long script and thank you for your help in advanced)
Note: The codes are Lua (which are used on the ROBLOX Studio Platform)
First off, thanks for writing out a clear and concise question. Most of the questions under this tag are all over the place :).
I would verify that you LocalScript instance is being properly initialized inside of the Player instance that is sitting down. Looking over your LocalScript's code, I don't see any errors that would cause the GUI not to function (if the LocalScript is being initialized properly), which leads me to believe the LocalScript isn't even running. If you are loading the LocalScript into the player on spawn (so, putting it into the PlayerScripts folder), there's your problem. This would cause the GUI to not be set up properly, as it hasn't been inserted into the players PlayerGui instance yet, so the LocalScript won't be able to hook up the MouseButton1Click events or anything of that sort.
So, make sure that the LocalScript starts running once the player sits in the seat (use the regular Script that displays the GUI to also move a copy of the LocalScript into the player). This will (hopefully) initialize the LocalScript properly, and allow it to hook into the GUI.
If this doesn't work, check your output window for any blatant errors, and post em here.
Cheers!
-widoreu

wxPython inactive state?

Right now I'm working on a program using wxPython, and I want to make a GUI in which under certain conditions, the widgets become inactive and the user can't interact with them. I'm pretty sure I have seen this in programs before, but I can't come up with a specific example. Is there a way to do this in wxPython? I have no idea what the technical name for this is, so even giving me that would be helpful.
When you say "the objects", what do you mean? If you mean a wx Frame, then you can call Frame.Freeze() to disable the frame, and Frame.Thaw() to unfreeze it. If you want to create a new dialog that must be interacted with and make all background windows unuseable, you can call Dialog.ShowModal(). Finally, many widgets have a Widget.Enable() function, to which you can pass True or False depending on if you want to enable it or not.

Raspberry Pi escape character

I am trying to create a shell script in which I automatically run a video fullscreen.
It has no way to quit unless I shut down the Raspberry Pi.
What is a small script I can use to bind something like "!" to quit the application?
I searched Google for 'omxplayer exit fullscreen' and found this answer, originally posted by dom on the RaspberryPi forum:
Changing TV modes does lose any content on them (e.g. the console
framebuffer).
You can provoke the console framebuffer to be recreated with: fbset
-depth 8 && fbset -depth 16
Add that to the end of a script that launches omxplayer.
(for extra points read the depth before launching omxplayer and set it
back to original value afterwards)
You might also want to check this issue report on omxplayer's GitHub.
I am not sure if this will work but potentially you could use a tkinter window that you make non visable.
#import the tkinter module for the GUI and input control
try:
# for Python2
import Tkinter as tk
from Tkinter import *
except ImportError:
# for Python3
import tkinter as tk
from tkinter import *
def key(event):
#create a function to control closing the window in this case
if event.keysym == 'Escape':
#this currently closes the window however you could add to root.destroy() with
#the relevant command for closing the video.
root.destroy()
#initiate root window, remove it from view, bind all keys (you could just
#bind '<Escape>' if preffered
root = Tk.tk
root.withdraW()
root.bind_all('<Key>', key)
i am aware this isnt a specific design to your problem however it will let you bind the escape key like you wished. this stopped the whole application in my example however you may have to include extra lines to ensure each part of your application is correctly ended.

Building a gui app using wxPython

Am building a gui app using wxpython,i have created two separate windows in two different files,i.e app1.py and app2.py,I want to open the 2nd window(app2.py) using a button click on first window(app1.py). How do i achieve this. It would be great if someone can help,Thanks!!
I am assuming you're using wxPython's definition of "window" (i.e. anything that is actually viewable in the GUI), not the regular definition of "separate box with stuff in it and an X button in the corner" (which wxPython calls a "frame")
When I was writing my first wxPython GUI I was faced with a similar probem. I wanted actions in one panel to affect the data shown in another. My first solution was to write methods that blindly passed the request to the panel's parent until it reached the top level (my "main frame", if you will). This worked, but obviously it's a terrible solution.
My second solution was to use wxPython Events, just like catching button presses for a wx.Panel. My idea was I would create a button in one frame and bind wx.EVT_BUTTON with that button's ID in another. I made custom Events and CommandEvents. While trying to implement this solution I discovered that do not propogate and that CommandEvents will only propogate through the parents. So, if left uncaught a CommandEvent will eventually hit your "main frame" but it will never hit the second panel unless that panel happens to be a parent of where the Event came from. Obviously, this won't work for you since the parent can't be hidden but a child be visible. Plus it would leave your main frame cluttered with code and methods to delegate commands.
Finally, I found the answer! wx.lib.pubsub! Basically, you bind the button press in app1.py, just like normal. Now, in the handler you use pubsub to publish a custom message. Next, in the init for app2.py, you use pubsub to subscribe to your custom message (I like having a "message.py" where I declare all my messages as constants). When you subscribe to a message you assign a handler method, just like for events.\
Read this: http://wiki.wxpython.org/WxLibPubSub
Since you said you wanted the window to APPEAR, maybe in the init method for app2.py you call the Hide() method then when you receive the message, call Show().
When I implemented this in my application I was using wxPython2.8. I am now using 2.9 so I don't know if this is still necessary but I found that I need the top import in order to call pubsub.subscribe() and pubsub.sendMessage(). I kept getting errors otherwise. I can't remember how I figured this out, I think I saw it in a code sample and added it in a desperate attempt to make my code work. I suggest you read the documentation I linked above and try to implement your code WITHOUT that import first.
import wx
from wx.lib.pubsub import setupkwargs #I need this to force pubsub to work. I don't know why.
from wx.lib.pubsub import pub
ID_MYBUTTON = wx.NewId()
class App1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init___(self, parent)
button = wx.Button(self, ID_MYBUTTON, "Show App2")
self.Bind(wx.EVT_BUTTON, self.handleButton, id=ID_MYBUTTON
def handleButton(self, event):
pubsub.sendMessage("mybutton.pressed") #send the message
class App2(wx.Window):
def __init__(self, parent):
wx.Window.__init__(self, parent)
self.Hide() #I don't want to be seen yet
pubsub.subscribe(self.gotMessage, "mybutton.pressed") #listen for the message
def gotMessage(self):
self.Show() #Now I want to be seen!
-----EDIT-----
I found this SO question that might help: Creating child frames of main frame in wxPython
Please tell us in detail what you're trying to do? wxPython also has "dialogs" which would be better than a frame if all you're doing is showing the user a message or asking for a bit more information. http://wxpython.org/docs/api/wx.Dialog-class.html
It really depends on what you're trying to accomplish. We can't help you unless you explain what problem you're trying to solve.
-----EDIT AGIAN-----
It's looking like the person asking the question did indeed want a wx.Dialog. For a tutorial, see http://zetcode.com/wxpython/dialogs/ Also see Python WX - Returning user input from wx Dialog

MATLAB: Prevent figures from being made active

I have a rather large routine which will can run for a couple of hours. Here and there it creates a figure, plots something to it and saves that Figure.
As I have only one PC, I would like to continue to work with that machine. The problem is that whenever a new figure is made, MATLAB becomes the active application again.
Is there any way to tell MATLAB or Windows that MATLAB should not be allowed to set itself to active?
I saw that one possibility is to run a MATLAB script totally in the background (like that). But that is a little bit too unsupervised, as I would like to be able to switch to the MATLAB window and check the output to the command window.
Any ideas? If there is a general solution for Windows that prevents that other Applications to become active would also be cool!
You can overload the figure function as following in order to prevent figure poping up:
a = figure('visible','off');
I hate to state the obvious, but you could always store the data you want to plot until the end.
Now, you're going to tell me that some of that data is subroutines and doesn't get passed back to the main routine. OK. So, the solution to that would be to write a "Store_Plot_Data" class with a method that would write into memory the data, the #plot_function_name (for 3D, scatter, etc.), the axis label strings, etc. Then you would create one instance of this class in your main routine and to ensure visibility of this one instance to all subroutines you could do any of the following:
use a global variable as your single instance ... OK, not so elegant,
implement the Singleton pattern, or
pass all subroutines the handle to that one instance of the "Store_Plot_Data" class.
If there is a general solution for Windows that prevents that other
Applications to become active would also be cool!
In Windows 7, this worked for me:
http://pcsupport.about.com/od/windowsxp/ht/stealingfocus02.htm
Set "HKEY_CURRENT_USER\Control Panel\Desktop\ForegroundLockTimeout" to 30d40 (hex).
If you want all figures to not show.
set(0,'defaultFigureVisible','off');
In the beginning of your script do:
set(0, 'DefaultFigureVisible', 'off');
set(0, 'DefaultFigureWindowStyle', 'docked');
Dock the Matlab figure window and maximize any other application (Excel, Word etc.) you are working with in front of Matlab.
Then you can continue to work without being interrupted by figures blinking on your face.

Resources