So I have been using Tkinter and I have it set up with a base background. I then open a file that adds my buttons and everything else. I will then have more files for the login and the game so on so on.
My main issue is closing the file. I could just use place.forget but what I really want to do is close the active file. I tried using exit() but all that does is close the window. so in short i want to close the file with the buttons but not the background file.
file_name = background.py
from tkinter import *
from tkinter import messagebox
top=Tk()
top.geometry("700x500")
top.resizable(False,False)
c=Canvas(top,bg="gray16",height=200,width=200)
c.pack()
filename=PhotoImage(file="mountain.png")
background_label=Label(top,image=filename)
background_label.place(x=0,y=0,relwidth=1,relheight=1)
canvas=Canvas(width=400,height=330)
canvas.place(x=150,y=70)
exec(open("./Home_screen.py").read())
top.mainloop()
this basically creates a graphical background and main window
file_name = Home_screen.py
from tkinter import *
from tkinter import messagebox
def rem():
loginPage.place_forget()
loginPage=Button(text="Login", width=20, command=lambda:[rem()], border=2)
loginPage.place(x=271,y=270)
what I would like to do is remove the .place_forget and replace it by closing Home_screen.py
Related
I am trying to show a tkinter Toplevel window, have the script sleep for one second, then continue running. However, the sleep function always occurs before the toplevel window appears. I'm doing this because I want to show an indication to the user that a label text has been copied on click.
I am planning to have the Toplevel show, sleep for one second, then destroy it. However, as it is currently, the sleep occurs first and then the Toplevelshows and is destroyed immediately too fast for the user to perceive.
Is there a way to have these events happen in my desired order? Here is a script demonstrating what I'm dealing with:
from tkinter import *
from tkinter import ttk
import time
root = Tk() # Creating instance of Tk class
def make_new_window(event):
new_window = Toplevel()
clabel = Label(new_window, text = "Copied")
clabel.pack()
time.sleep(1)
l = Label(root, text = "Example text")
l.pack()
l.bind('<1>', make_new_window)
root.mainloop()
I'm developing a program with Python and tkinter that begins with the user adding a text file from their directory. I had built a GUI with tkinter that provided a button to be pressed and a popup window for them to select their file--it was working fine, and then suddenly when I tried to run it it would begin "Not Responding" when I pressed the button meant to launch the popup window.
I am running Python 3.7.3 on Windows 10 in Jupyter notebooks; the tkinter version is 8.6. I have 8 GB of RAM but I'm not using more than 80% of it.
I've tried looking at some similar Stack Overflow questions like the ones here and here:
windows thinks tkinter is not responding
Python tkinter askopenfilename not responding
Trying askopenfilenames() didn't work; neither did adding root.update() or %gui tk.
Here is the code I'm working with:
import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import *
root = Tk()
topFrame=Frame(root)
topFrame.pack()
middleFrame=Frame(root, width=200, height=250,
highlightbackground="yellow",
highlightthickness=3,
borderwidth=2,
relief=RAISED)
middleFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack()
ourdirectory=[]
def load1():
f1 = askopenfilename(filetypes=(('TXT files','*.txt'), ('All files', '*.*')))
ourdirectory.append(f1)
mylab = Label(topFrame, text="Hello and welcome!")
mylab.pack()
button = Button(bottomFrame, text="Add File", command=load1)
button.grid(row=5, sticky=W)
root.mainloop()
When the "Add File" button is pressed, I expected the program to launch a popup window where the user can navigate in the file directory. Instead, the title text just fades and it doesn't respond--and clicking on the window causes it to announce that it's stopped responding. (This also consistently causes the Jupyter kernel to die.) Still, when I run the code but close the main window without pressing the "File Add" button, it closes fine and nothing freezes or breaks.
I have encountered the same issue when running some code that would open, modify, and overwrite an image. Everything worked as expected until suddenly askopenfilename() started Not Responding and not opening the file dialog window.
I have attempted changing the initial directory of askopenfilename() and calling the method from a separate file and the command line without success.
One of my open windows was a directory (not the same as I was working with) on which file thumbnails failed to load and files could not be opened. Upon closing the window, the screen 'reloaded,' certain programs opened (such as sticky notes, which was previously closed but which typically opens upon restarting), and the method resumed normal functionality.
I'm making a simple GUI using Python 3.7.3 and tkinter to open and close windows applications. I'm not able to find a way to close a running program using an onscreen button. I need the 'close' button to do something else as well, hence the simply using 'x' button (which is next to the minimize and maximize) won't work for my case.
from tkinter import *
import os, subprocess
root = Tk()
root.geometry("300x300")
def OpenCalc():
app1 = os.startfile("C:\Windows\System32\calc.exe")
def CloseCalc():
os.close(app1)
# or
# os.closefile("C:\Windows\System32\calc.exe")
b1=Button(root, text="Open Calc", command=OpenCalc).pack()
b2=Button(root, text="Close Calc", command=CloseCalc).pack()
root.mainloop()
I am currently implementing a program that uses many tkinter frames and while subframe is being opened I want the superframe to be locked for the user (otherwise things will not work out). After some research I found the grab_set and grab_release method which worked quite fine.
However once the subframe (instanciated by Toplevel) calls the askstring the grab is "losed" and the user can interact with the superlevel window again. An example would be this (very simplified code):
import tkinter as tk
import tkinter.simpledialog
root = tk.Tk()
def open_sublevel():
tl = tk.Toplevel(root)
def ask():
print(tk.simpledialog.askstring("askstring", "askstring"))
tk.Button(tl, text="ask", command=ask).pack()
tl.grab_set()
root.wait_window(tl)
tl.grab_release()
print("release")
tk.Button(root, text="asdf", command=open_sublevel).pack()
tk.mainloop()
Once the user opens the subframe by clicking "asdf" the frame containing "asdf" will be locked for the duration while the subframe is opened. However once the user selects the "ask"-Button in the subframe this "lock" somehow disappears.
According to the notes in the tkinter library:
A grab directs all events to this and descendant widgets in the application.
I am not able so far to find any documentation that would explain why the grab_set() is falling off after you finish submitting your askstring but I would imaging it is because once the widget is gone the grab_set() falls off. Just like if you were to close out the Toplevel window.
In this case tl.grab_release() does not appear to be needed as grab releases once the window closes.
From what I have tested if you reset the grab_set() after the askstring is done then it will still work properly.
You need to simply add tl.grab_set() just below print(tk.simpledialog.askstring("askstring", "askstring")).
Modified code below:
import tkinter as tk
import tkinter.simpledialog
root = tk.Tk()
def open_sublevel():
tl = tk.Toplevel(root)
tl.grab_set()
def ask():
print(tk.simpledialog.askstring("askstring", "askstring"))
tl.grab_set()
tk.Button(tl, text="ask", command=ask).pack()
print("release")
tk.Button(root, text="asdf", command=open_sublevel).pack()
tk.mainloop()
setting the parent for simpledialog will make simpledialog take focus
x = simpledialog(parent = window_x, title = z etc.)
this will make sure x takes focus and not withdraw
i am opening a file in a gui label.
the file i have is big, i need a scroll for it in gui frame.
the code i am using for loading the file and displaying it in a frame is
from Tkinter import *
tk = Tk()
tk.title("Vulnerability Report")
f = open("hola.txt", "r").read()
Label(tk, text=f) .grid(row=0)
tk.mainloop()
Here is the screenshot: I want a scroll here through which i can read the whole file up and down
I have no idea why you are using Canvas instead of Frame and why you are loading a large file on a Label, but you cannot attach a Scrollbar to a label. Tkinter has a ScrolledText widget. Do from tkinter.scrolledtext import ScrolledText and use it as one would a Text widget.