So I made the Main Menu in one Tkinter File and I made an app in another file. In the main menu, I have a button, when I click that button it should open the app but NOT SHOW THE MAIN MENU.
File 1
from tkinter import *
root = Tk()
root.geometry("600x600")
btn = Button(root, command=#add one later)
And in file 2 I have my app and when I click on the button it should open the app and hide the main menu. Can I do this through frames?
NOTE: THE APP IS ANOTHER FILE
As far as I have understood your question, there could be 2 things that you might be asking, 1st if you want to launch a new window and destroy this, you can call root.destroy() followed by calling the other file by using import file_name that has another Tk(). 2nd if you want to pack/update a Frame then make sure that the other file is within a definition which is not called there, also in the second file use the same terminology as you have used in the first file or you can pass these parameters through the function call. Now you can do
menu.destroy()
import file_name
file_name.function_name(arguments)
Related
I am trying to make a Tkinter script to select files through Windows File Explorer. I don't need the Tkinter window to show, just the File Explorer interface.
import tkinter
from tkinter import filedialog
import os
window = tkinter.Tk()
#window.geometry("1x1")
window.withdraw()
def open_files():
files = filedialog.askopenfiles(mode='r')
global filenames
filenames = [os.path.abspath(file.name) for file in files]
window.destroy() # analysis:ignore #says "window" is undefined becasue of "del window" below
window.after(0, open_files)
window.mainloop()
del window
The first time I run this in Spyder, if window.withdraw() is not commented out, the console just shows runfile(*my_file_name*) and the code does... something... in the background, but nothing seems to actually happen. Nothing changes on-screen, but I cannot type in the console so I know the code is running.
If I open a new console tab and run the code with window.withdraw() commented out, everything works, and the Tkinter GUI is visible. If I then run this code again in the same tab, with window.withdraw() not commented out, then the code works as intended, with only the File Explorer window opening up, and the Tkinter GUI staying hidden. This is the case even if I click the "Remove all variables" button in Spyder, so as far as I understand the code is not saving any variables that allow it to run properly after the first time.
My question is, why does this code work the 2nd, 3rd, 4th, etc. time I run it, but not the first time?
I kept playing around, and changed -alpha to alpha and got this error:
TclError: wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"
So I ended up changing window.attributes('-alpha',0) to window.attributes('-topmost',True, '-alpha',0), and this works! It brings up File Explorer on the first run without showing the Tkinter window. Thank you #Thingamabobs for your help.
My final code is:
import tkinter
from tkinter import filedialog
import os
window = tkinter.Tk()
window.attributes('-topmost',True, '-alpha',0)
filenames = [os.path.abspath(file.name) for file in filedialog.askopenfiles(mode='r')]
window.destroy()
del window
I have a Tkinter window in the file gui.py. Upon the press of the Spacebar, I want to open another Tkinter window which is used to obtain an input from the user via the file imageinput.py.
So, I wrote the code to execute the run function of imageinput.py
def keyPressed(event, data):
if event.keysym == "space": image_run()
When I run this, I get the following error:
What is the best way to open such another Tkinter window this way?
Without knowing more of your code, you would create a new "top level" widget and use that widget like you used the original root top level window ( root = tkinter.Tk()) as the parent of whatever widget hierarchy you create. So...
def image_run(parent, *args, **kwargs):
top = tkinter.Toplevel(parent)
top.transient(parent)
canvas = tkinter.Canvas(top, ...)
:
:
Hope that helps!
I'm developing an interface with Tkinter that makes use of a file dialog with tkFileDialog.
I want to run a function immediately after the user has chosen a file from the dialog box.
With buttons, we have a command keyword from which we run a function (usually named def callback():). Is there a similar keyword for the file dialog or askopenfilename?
The askopenfilename function consists of the opening of a dialog, and returns immediately when the latter was closed (including when a file has been selected).
Put your callback right after this function to have it run right after the closing of the dialog.
For instance:
from tkinter.filedialog import askopenfile
fileDescriptor = askopenfilename()
print(fileDescriptor)
will open a file selection dialog, and as soon as the user has selected a file, the corresponding object that was created will be printed out.
I'm currently trying to make a small application with a GUI that pulls weather from a website and displays the results in a window. I've got it to work without the GUI and also with the GUI but when I wrote the latter it was all in one script and not very organized. Because it was so unorganized, I decided to make make a separate script that would draw the GUI when the class was called.
Part of the GUI is an 'Entry' box that can be added via Tkinter. The entry box stores it's content into a StringVar() and that content can displayed using .get(). This works fine and well when I wrote everything unorganized into one script but I can't for the life of me figure out how to pass this StringVar() from one method to another in my program. This is what it looks like:
from Tkinter import *
import Forecast
class Frames(object):
def __init__(self):
pass
def main_frame(self):
main = Tk()
main.title('WeatherMe')
main.geometry('300x100')
query = StringVar()
Label(main, text='Enter a city below').pack()
Entry(main, textvariable=query).pack()
Button(main, text="Submit", command=self.result_frame).pack()
main.mainloop()
def result_frame(self):
result = Tk()
result.title('City')
result.geometry('600x125')
Button(result, text="OK", command=result.destroy).pack()
result.mainloop()
Basically my goal is to have one window open when the program is launched with a label, an entry box, and a submit button. When a city is entered in the entry box and submit it clicked a new window will open displaying the results.
Because the entry is on the first window I need to pass the value of entry's StringVar() to the second window so it can then pull the data and display the labels. No matter what I try it doesn't seem to work, I either get a 404 error meaning something is wrong with that string making the link it tries to get a response from invalid or a concatenate error 'cannot concatenate str and instance objects'.
I've also tried saving StringVar() as a variable outside of either method but the issue with that is I need to then call another instance of Tk() before StringVar().
You are creating two separate instances of Tk. You shouldn't do that. One reason why is because of this exact problem: you can't share instances of widgets or tkinter variables between them.
If you need more than one window, create a single root window and then one or more instances of Toplevel. Also, call mainloop only for the root window.
I am trying to write a simple text editor using TkInter. I want it to have a menu bar like other text editors, where you can save, open another file, etc.
However, whenever I try to add a menu bar to my class, the program simply starts, hangs for about half a second, then exits. I have no idea why this is happening, or how to debug it. Here is my code.
#!/usr/bin/env python3
import functools
from tkinter import *
class mainWindow(Tk):
def initiate(self):
menuBarFrame = Frame(self).pack(side=TOP)
menubar = Menu(menuBarFrame)
menubar.add_command(label='Exit', command=quit())
root.config(menu=menubar)
mainloop()
win = mainWindow().initiate()
I tried adding .pack() to the line
menubar = Menu(menuBarFrame)
but it gives me the following traceback:
File "XML.py", line 14, in <module>
win = mainWindow().initiate()
File "XML.py", line 9, in initiate
menubar = Menu(menuBarFrame).pack()
File "/usr/lib/python3.4/tkinter/__init__.py", line 1977, in pack_configure
+ self._options(cnf, kw))
_tkinter.TclError: can't pack ".140664986043280": it's a top-level window
When I remove the code for the menubar, and just replace it with a simple button, the application works and starts fine. What could be causing the problem?
The menu needs to be a child of the root window, rather than a child of a frame. You don't need MenuBarFrame at all.
Also, take a look at this line:
menubar.add_command(label='Exit', command=quit())
You are instructing Tkinter to immediately call the quit() function, and assign the result to the command attribute of the menu command. I'm guessing that quit() actually quits rather than returning a reference to some other function. You need to change it to this:
menubar.add_command(label='Exit', command=quit)
Of course, the other glaring problem is that you don't actually define root anywhere.
You definitely don't want to call pack() on the instance of Menu. The correct way to attach the menu to the window is with root.config(menu=menubar), like you're already doing.