How To Make a Window in Python without GUI toolkits - python

I'm curious, as making a window with Tkinter really does seem to easy. Does Python have an alert thing similar to javascript by default?

There is no graphical alert in python. Tkinter is the default python GUI interface. If all you need is an alert, that's less than a dozen lines of code. Here's a python 3 example:
import tkinter as tk
import tkinter.messagebox
def alert(message):
root = tk.Tk()
root.withdraw()
tkinter.messagebox.showwarning("Alert", message)
root.destroy()
alert("Danger Will Robinson!")

Related

Tkinter window with overrideredirect randomly pops up when any other application is opened

I've been trying to make use of the overrideredirect() function in tkinter, but I'm facing a small issue.
Here's a sample code snippet:
from tkinter import *
root = Tk()
root.geometry("500x500+200+200")
root.overrideredirect(True)
mainloop()
Here when I press Window + D the window minimizes like it's supposed to, but if I restore any minimized app or open a new application (like Chrome), the tkinter window gets restored too instead of staying minimized.
Is there any way to fix this problem? It would be great if anyone could help me out.

Tkinter dialog box with user input AND custom buttons

Can't find this anywhere else on stack overflow.
I want to create a dialog box (on top of my main window) in tkinter that has message, a text input field and some custom buttons.
I know I can do
import tkinter as tk
from tkinter import simpledialog
master = tk.Tk()
user_input = tk.simpledialog.askstring("title", "message")
and then take the user_input from there. However, how can I make the buttons have custom text and also bind them to my own functions?
Otherwise, I was thinking of making my own dialogbox class. But that's a lot of work. Is there any way I can do this natively using any of tkinter' modules?
Regards
Hugo.

Python 3.3 tkinter.filedialog.askopenfile() leaves an unresponsive Tk window

I'm trying to have the user select a file through a pop-up directory browser. Looking things up suggested that I use filedialog.askopenfile(), which works but leaves a small unresponsive tk window that I'd rather not have because any attempt to interact with it causes the kernel to crash and need to be restarted.
Does anyone have any solutions or alternatives? Thanks.
This works fine:
from tkinter import *
root = Tk()
root.withdraw()
filedialog.askopenfile()

Can I get messagebox's icon="warning" noise on a custom Tkinter Toplevel() window?

I like the warning noise that messagebox provides out of the box (no pun intended), as in the GUI below.
from tkinter import messagebox
messagebox.showinfo("my title", "my message", icon="warning", parent=None)
However, I need more customization than messagebox offers, so I've created my own window. Is there a way to include the same warning noise with a Toplevel() window?
I'm using Python3 and Tkinter 8.5.
I think you would need to manually sound it
import Tkinter
Tkinter.Tk().bell()
or maybe you can just do SomeWindow.bell() (to be honest im not overly familiar with tkinter)

getting keyboard back from tkinter

I'm working on a graphical app, and at the start of the run I'd like to ask the user a single configuration question. The graphical framework (Panda3D) has ugly default dialog boxes, so I'd like to use something like tkInter to provide a modal dialog. I tried this:
import Tkinter
import tkMessageBox
root = Tkinter.Tk()
# hide the root window
root.withdraw()
config.PLAY_MUSIC = tkMessageBox.askyesno( "My App",
"Would you like this app to play music from your iTunes collection?" )
root.destroy()
This does what I want it to, but it appears to route all further keyboard events to tkInter rather than my Panda3D app. I don't need to do anything further with tk after this dialog.
I can put the tk dialog into a separate app that chains onto mine, I suppose, but I'm wondering if there's a way to kill tk and get the keyboard back without exiting my app entirely.
Update: Tried root.quit(), which does seem to get the keyboard back, but I get a "Fatal Python error: PyEval_RestoreThread: NULL tstate" crash on exit from my program, which isn't ideal.
Have you tried:
grab_release(self)
Which does: Release grab for this widget if currently set.
Where "A grab directs all events to this and descendant
widgets in the application."
as in:
root.grab_release()
Hope you haven't tried this one.

Categories