I am currently stepping through the tkinter tutorial at python-course.eu. Is it possible to close an entry widget without killing the program? What I am trying to do is incorporate the tkinter entry box into a pygame program such that the program asks for the players name via an entry box and then closes once the text has been entered. The game should then continue. Is this possible?
What I would like to do is:
-create a pygame surface
-open a tkinter entry widget on top of that surface
-get the users name
-close the tkinter widget
-write text using pygame onto the surface that incorporates the user's name
What is stumping me is the fact that the tkinter examples on python-course.eu all end with a mainloop() statement while I would like pygame to have an event loop so that I can expand the program. I anticipate that the widget creation would occur prior to dropping into the event loop. This is where I am stuck :-(
rather than trying to mix two GUI libraries in the same window, if a prompt is acceptable you could use:
import tkinter as tk
from tkinter import simpledialog
root = tk.Tk() # needed to prevent extra window being created by dialog
root.withdraw() # hide window as not needed
username = simpledialog.askstring('Username', 'Enter username:')
root.destroy()
Related
I have found out a method to get a button to align with ttk.Notebook.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
notebook = ttk.Notebook(root)
ttk.Button(notebook).pack()
notebook.pack(fill='both', expand=1)
label = ttk.Label(notebook, text='Text', font='Arial 50')
notebook.add(label, text='Tab')
root.mainloop()
However, this caused a problem that the geometry manager only displays the button. I have to maximise the window in order to see all the contents.
In my another bigger gui, this is just a little bit of the window. I can’t see the content inside the notebook even I maximise the window.
So, how can I get it working? Thanks for any ideas!
This is due to the fact that you're calling pack on a button inside the notebook. The button is empty so it has a size of zero, and calling pack on the button causes the notebook to shrink to the size of the button.
You shouldn't ever call pack or grid on widgets that are direct children of a notebook, they should only ever be added with the add method of the notebook.
It's not clear where you want the button, but it shouldn't be a child of the notebook. If you make it a child of the root window (eg: ttk.Button(root), you can add it to the root window with pack to get it to be either above, below, or to one of the sides of the notebook.
I'm creating a Tic-Tac-Toe program using tkinter, in which I would like to take the name of the user using entry and the next name to be given in a separate window, but before that I would like to automatically close the previous window.
I used a common variable called root (mainloop also) for displaying all the windows. Once I destroy the root, the whole program stops. Should I name a separate variable with tk.Tk() to proceed and use it in other windows? I just can't understand.
Can anyone help me...
Yes, destroying the root will close the entire app.
If you want to close windows while keeping the app running, you can use a tk.Toplevel to pop a window open, and be able to close it while continuing other operations.
maybe like this:
import tkinter as tk
def popup():
p = tk.Toplevel(root)
p.title('popup')
tk.Label(p, text='I will self destroy in 3 seconds').pack()
p.after(3000, p.destroy)
root = tk.Tk()
btn = tk.Button(root, text='pop a new window', command=popup)
btn.pack()
root.mainloop()
I'm new to stack exchange and am inexperienced with python. I am using python3 and have used SourceFileLoader from importlib.machinery to run another script(which uses tkinter) from my main script. This initially caused an error covered in this question: tkinter.TclError: image "pyimage3" doesn't exist
The solution worked for me in resolving the error, and now the child script runs as intended, except that using the Toplevel() function now also causes the app to create an empty root window - Tk() which I want to hide. I have looked at a number questions' solutions that have not worked: How do I get rid of Python Tkinter root window?
Hide the root window when a Toplevel window is opened and make it reappear when the Toplevel is destroyed
Here is a sample of my code:
from tkinter import *
from PIL import ImageTk
from importlib.machinery import SourceFileLoader
root = Toplevel()
background = Tk()
...
def Puzzle5():
root.overrideredirect(1)
frame = Frame(root, width=320, height=160, borderwidth=2, relief=RAISED)
frame.pack_propagate(False)
frame.pack(side=TOP)
frame1 = Frame(root, width=500, height=150, borderwidth=2, relief=RAISED)
frame1.pack_propagate(False)
frame1.pack(side=BOTTOM)
image = ImageTk.PhotoImage(file="/home/pi/Media/arrowup.png")
image1 = ImageTk.PhotoImage(file="/home/pi/Media/arrowdown.png")
...
background.withdraw()('0x0+0+0')
root.geometry('644x450+150+50')
root.mainloop()
def close():
root.destroy()
background.destroy()
Puzzle5()
Widgets exist in a hierarchy. At the top of that hierarchy is the root window. For any tkinter widget to exist, there must first be a root window.
You can create the root window by creating an instance of Tk. If you do not, then the first time you create a window a root window will be created for you.
Now, consider this code:
root = Toplevel()
background = Tk()
Toplevel is not a root window. For it to exist, there must first be a root window. Since you did not create one, tkinter will create one for you. So, you get a root window, and then you get the instance of Toplevel.
Then you create another root window with the second line, resulting in three windows. Even when you hide background with background.withdraw(), you still have the original root window visible.
The simple solution is to reverse those two lines of code. Create the root window first, and the Toplevel second. Then you only have a single root window, and you can hide it if you wish. However, as the answer to How do I get rid of Python Tkinter root window? explains, an even better solution is to not use a Toplevel at all, but instead put your widgets in root.
I have run into a problem durring abaqus programming with phyton 2.7.
I'm using Tkinter and making a window with a lot of controls on it.
My main problem is that durring my plugin window is opened, the user needs to click on abaqus menuitems, browse modells, etc. So using the main program while my plugin still works on screen.
If I do create my Tk window without thread, than when the user clicks on abaqus main windo functions while my plugin is opened, then abaqus will not respond or crash with "LoadlibraryA error 193"
(example: while plugin runs and user clicks on Viewvport menü/ViewPort Annotation Options then he/she wont be able to change tabs)
If i do create my Tk window inside a thread, then the al the Tk window controls will only responds the mouse events after I leave the Tk window with my cursor.
(example: I make 2 notebook page and after start i click on the not selected one. then nothing happens until my mous inside the Tk window, but as soon as i move it out, the click takes effect and the tab changes...)
The threaded version of my code:
import threading
class pilotDB(threading.Thread):
def shutdown_ttk_repeat(self):
self.root.eval('::ttk::CancelRepeat')
self.root.destroy()
def __init__(self):
import threading
threading.Thread.__init__(self)
def refresh(self):
self.root.after(50, self.refresh)
def tabpage(self):
import ttk
import sys
self.notebook = ttk.Notebook(self.root)
self.tabpage_tab1 = ttk.Frame(self.notebook,width=400,height=500)
self.tabpage_tab2 = ttk.Frame(self.notebook,width=400,height=500)
self.notebook.add(self.tabpage_tab1, text='Tab1')
self.notebook.add(self.tabpage_tab2, text='Tab2')
self.notebook.place(x=30, y=40)
def run(self):
import Tkinter
self.root = Tkinter.Tk()
self.root.protocol("WM_DELETE_WINDOW", self.shutdown_ttk_repeat)
self.tabpage()
self.root.after(1000, self.refresh())
self.root.mainloop()
app = pilotDB()
app.start()
app/pilotDB has no function named "start" (last line of the code posted). If I put a call to self.run() in init, and delete threading, then the program works as expected, i.e. opens a window and displays 2 tabs and the user can switch between tabs, all the time the mouse is in the window as is normal. Note also that the refresh() function does nothing but call itself repeatedly. You should try to find the offending code by first commenting the lines for the "WM_DELETE_WINDOW", shutdown_ttk_repeat and the call to execute the function tabpage() which leaves a basic window. Then uncomment one and run it, and repeat until you find the error.
I'm wondering if there's a way to make clickable text in Tkinter. Maybe like you would see on a title screen of a game, and where you hover your mouse over the text and it changes color/hightlights itself. All I need the click to do is execute another function.
Are either of these things possible? Thanks!
you are looking for tkinter's events:
tk_widget.bind("<Button-1>",CALLBACK)
The call back needs to take an event argument which is a dictionary containing information about what triggered the event.
This can run into issues with widgets that overlap such as windows in a canvas or labels sometimes triggering the callback for the window behind it.
For hovering the mouse over a widget the event is called "<Enter>" and moving mouse out of widget region is called "<Leave>" for highlighting text effect, if you just want to capture a click anywhere on a window then on the root call root.bind_all("<Button-1>",CALLBACK)
source: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/events.html
example:
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
def change_case(event=None):
new_text = str.swapcase(lab["text"])
lab.config(text=new_text)
def red_text(event=None):
lab.config(fg="red")
def black_text(event=None):
lab.config(fg="black")
root = tk.Tk()
lab = tk.Label(root,text="this is a test")
lab.bind("<Button-1>",change_case)
lab.bind("<Enter>",red_text)
lab.bind("<Leave>",black_text)
lab.grid()
root.mainloop()
hope this helps :)