This is my first post on here, so bear with me as far as post etiquette goes. I've been struggling to get rid of the base Tkinter window that appears when I'm using the askopenfilename function of Tkinter. I've tried using Withdraw and destroy (in combination and alone) to fix this issue, but it seems to leave my code stuck in a loop and unable to continue to the next sections.
I have seen several solutions to this in Python 2, but I have no idea how they translate to python 3. Tkinter isn't a module I have a lot of experience with, so it is likely a simple oversight I am making.
Any suggestions or comments are much appreciated
Here is a sample of my code I am using (CSV module is for another section of my code)
import csv
from tkinter import filedialog
from tkinter import *
root= Tk()
root.filename= filedialog.askopenfilename(initialdir = r"\Users",title="Select A File", filetypes= [("Csv Files","*.csv")])
root.mainloop()
Use withdraw to hide root window, deiconify to show root window
import csv
from tkinter import filedialog
from tkinter import *
root=Tk()
# Hide Window
root.withdraw()
filename= filedialog.askopenfilename(initialdir = r"\Users",title="Select A File", filetypes= [("Csv Files","*.csv")])
# Show Window
root.deiconify()
root.mainloop()
Related
I have coded a tkinter application using the usual tk widgets. I have learned that the tkinter.ttk widgets have a more modern look and make everything better. But now alot of my code does not work. Here is a small one which I don't understand why.
Here is tkinter
And tkinter.ttk
The difference is Type of Project is not there... I got around this by just putting Type of Project in the options it doesn't show it either(But how would I be able to like preset it).
Here is tk code:
om1str = StringVar()
om1str.set('Type of Project')
om1list = ['Photo Editing', 'Video Editing']
om1 = OptionMenu(new, om1str, *om1list, command = Create_New.more_options)
And ttk code:
om1str = StringVar()
om1list = ['Type of Project', 'Photo Editing', 'Video Editing']
om1 = OptionMenu(new, om1str, *om1list, command = Create_New.more_options)
Here is another example. Before swapping from tkinter to tkinter.ttk this piece of code worked but now does not. (I had this exact same code for the photo editing option two)
if choice == 'Video Editing':
print('hi')
for widgets in new.winfo_children():
if widgets.winfo_class() == 'Frame' or widgets.winfo_class() == 'Button':
widgets.destroy()
And now this kinda happens (The name does not duplicate because it is not in the same def Create_New.more_options())
How would I be able to delete the frame and the button without removing all contents of the tk.Toplevel()
I know how to set the bg and fg color(was really confused) after some quick google searches.
Is there any smooth way to go from tkinter to the tkinter.ttk widget styles without having to do a little 2 much work.
I'm running python 3 code in background which should show a popup window in some situations. I'm using tkinter for this:
import tkinter as tk
from tkinter import messagebox
def popup(message, title=None):
root = tk.Tk()
root.withdraw()
root.wm_attributes("-topmost", 1)
messagebox.showinfo(title, message, parent=root)
root.destroy()
popup('foo')
The ok-button in this infobox should get the focus automatically when popping up. Sadly I'm not able to do this. I tried root.focus(), but it does not help. Any ideas how to solve that? TIA
BTW: The code should be platform independent (Linux and Windows).
Edit:
Maybe I missunderstood the focus keyword and I should clarify my question:
root = tk.Tk()
root.focus_force()
root.wait_window()
When calling the code above the root window is active, even if I worked in e.g. the browser before. Is this also possible for messagebox.showinfo? Adding root.focus_force() in the popup function does not help.
Is this even possible? Or is it necessary to create my own root window? I really like the appearance of the messagebox with the icon.
Edit 2:
Here is a video: https://filebin.net/no195o9rjy3qq5c4/focus.mp4
The editor is the active window, even after the popup was shown.
In Linux I it works as expected.
You can use the default argument in the messagebox function.
default constant
Which button to make default: ABORT, RETRY, IGNORE, OK, CANCEL, YES, or NO (the constants are defined in the tkMessageBox module).
So, here is an example to highlight the "ok" button.
import tkinter as tk
from tkinter import messagebox
def popup(message, title=None):
root = tk.Tk()
root.withdraw()
messagebox.showinfo(title, message, parent=root, default = "ok")
root.destroy()
popup('foo')
Hope this helps!
I have a problem that annoys me. I am currently building a small app with a Tkinter GUI.
On the front page, I want some introductory text in either a text or a scrolledtext widget. The code examples I've come across uses keywords such as INSERT, CURRENT and END for indexation inside the widget.
I have literally copy pasted the below code into my editor, but it doesn't recognise INSERT (throws error: "NameError: name 'INSERT' is not defined"):
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
I can get the code to work if I change [INSERT] with [1.0], but it is very frustrating that I cannot get INSERT to work, as I've seen it in every example code I've come across
Use tk.INSERT instead of only INSERT. Full code is shown.
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(tk.INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
You don't need to use the tkinter constants. I personally think it's better to use the raw strings "insert", "end", etc. They are more flexible.
However, the reason the constants don't work for you is that you're not directly importing them. The way you're importing tkinter, you need to use tk.INSERT, etc.
INSERT could not be used directly.
You can use it in the past just because you used this in the past:
from tkinter import * # this is not a good practice
INSERT,CURRENT and END are in tkinter.constants.Now in your code,you even didn't import them.
If you want to use them,you can use
from tkinter.constants import * # not recommended
...
txt.insert(INSERT,'You text goes here')
Or
from tkinter import constants
...
txt.insert(constants.INSERT,'You text goes here') # recommend
If didn't want to import them,you can also use:
txt.insert("insert",'You text goes here')
Edit:I found in the source code of tkinter,it had import them,reboot's answer is also OK.
In the code below, the first dialog box gets focus immediately, so the user can just type an answer and press enter. In the second one, that doesn't seem to happen when running in Windows. Running Raspbian 9, both windows get focus when they open.
Is there any way I can get both windows to get focus when they open in Windows?
import tkinter as tk
from tkinter import simpledialog
root = tk.Tk()
root.withdraw()
answer1 = simpledialog.askstring("Test1","This one gets focus when it opens",parent=root)
answer2 = simpledialog.askstring("Test2","This one doesn't",parent=root)
I have watched this question for a few days now hoping someone might shed some light on this issue. I'm running Python 3.6.5 under windows 10 and get the same problem.
I have tried several different things but it seems Microsoft does things their own way. I have finally found a thing that works, but only if you don't hide the root window:
import tkinter as tk
from tkinter import simpledialog
root = tk.Tk()
#root.withdraw() # This does not work if you hide the root window
root.update_idletasks()
answer1 = simpledialog.askstring("Test1","This one gets focus",parent=root)
root.update_idletasks()
answer2 = simpledialog.askstring("Test2","This one doesn't",parent=root)
I found the following worked (with a small flicker of the root window on destroy()):
root = tk.Tk()
root.withdraw()
filename = filedialog.askopenfilename()
root.deiconify()
root.destroy()
I'm working with Tkinter in Python 2.7 on Windows 7, and found the need to create a popup box with a tree-style list of checkboxes. I could not find this in Tkinter, or ttk. I did, however, find it in Tix in the CheckList widget. I got a working standalone example using Tix, but I cannot figure out how to add my Tix.CheckList to my ttk.Frame that controls my main program.
Surely I am not forced to use Tix framework from the ground up?
import Tix
import pandas as pd
import Tkinter as tk
class TreeCheckList(object):
def __init__(self, root):
self.root = root
self.cl = Tix.CheckList(self.root)
self.cl.pack(fill=Tix.BOTH, expand=Tix.YES)
self.cl.hlist.config(bg='white', bd=0, selectmode='none', selectbackground='white', selectforeground='black', drawbranch=True, pady=5)
self.cl.hlist.add('ALL', text='All Messages')
self.cl.hlist.add('ALL.First', text='First')
self.cl.setstatus('ALL.First', "off")
self.cl.hlist.add('ALL.Second', text='Second')
self.cl.setstatus('ALL.Second', "off")
self.cl.autosetmode()
def main():
root = Tix.Tk()
top = Tix.Toplevel(root)
checklist = TreeCheckList(top)
root.update()
top.tkraise()
root.mainloop()
if __name__ == '__main__':
main()
The above code works in a standalone program using all Tix widgets. However, when I try to implement this into my larger program, I receive a TclError: invalid command name "tixCheckList"
To simulate this in the standalone, I changed the lines:
root = Tix.Tk()
top = Tix.Toplevel(root)
to
root = tk.Tk()
top = tk.Toplevel(root)
I was hoping I could just implement a Tix.Toplevel, placing it on a tk.Tk() root, but same issue.
Am I only allowed to use Tix frames when using a Tix widget, or am I misunderstanding something? If anyone has good Tix documentation, I would LOVE whatever I can get. It seems good docs on it are few and far between. Or is this same functionality included in ttk and I've just overlooked it? It seems to be one of the only things left out.
I have just learned that apparently only root needs to be a Tix class. Since Tk, and therefore ttk, classes appear to be added to the Tix root just fine (since most of them extend the Tkinter classes anyway), this appears to be a "fix". So my problem may have been solved by changing just
root = tk.Tk()
to
root = Tix.Tk()
This did require that I pull Tix into a part of my program I wasn't wanting for encapsulation purposes, but I guess there's no other way.