tkinter not responding when a text file is opened - python

I have a tkinter program that has a button. The button's job is to open a text file on my desktop using notepad. Here is the code:
import tkinter as tk
import os
def open_file():
file = os.path.expanduser('~/Desktop/a.txt')
os.system("notepad.exe " + file)
root = tk.Tk()
button = tk.Button(root, text='open', command=open_file)
button.grid()
root.mainloop()
The code runs and the program is working, but the problem is that when I click the button, the text file opens but tkinter window will be not responding until I close the text file. I am using Python 3 and Windows 10.
Any help would be appreciated.
Thanks.

Just use: os.system("start notepad.exe")

Related

Tkinter window not opening in pycharm

I am eriting code in pycharm with tkinter but the window is not opening. May someone assist?
`
import tkinter
window = tkinter.Tk()
button = tkinter.Button(window, text="Do not press this button! >:-(", width=40)
button.pack(padx=10, pady=10)
`
i tried checking my script for bugs but nothing
This has nothing to do with Pycharm, but with tkinter library and how to use it.
You are missing 2 important stuff:
Button is in ttk.py file inside tkinter library: from tkinter import ttk
Execute the whole script with mainloop
Try this:
import tkinter
from tkinter import ttk # Import ttk file from tkinter library
window = tkinter.Tk()
window.title("Coolest title ever written")
button = ttk.Button(window, text="Do not press this button! >:-(", width=40) # Use Button from the import ttk file
button.pack(padx=10, pady=10)
window.mainloop() # Execute the whole script

Why tkinter filedialog is looking different after using a pyglet window?

I have used a tkinter.filedialog with the pyglet library. But an old file explorer pops up when I open that filedialog. Normally it opens the native/modern file explorer of windows, but it is showing this behavior after importing the pyglet module. Any solution to get back the new filedialog?
Here is a basic code to reproduce the issue:
from tkinter import Tk, Button, filedialog
import pyglet #Try commenting this import to test the filedialog
root= Tk()
root.geometry("200x100")
window = pyglet.window.Window(20, 20, "Pyglet_window") #Pyglet window
def open_file():
file=filedialog.askopenfilename()
btn= Button(root, width=10,bg="#FFFFFF",fg="black", text="Open File",command=open_file)
btn.grid(padx=50,pady=20)
root.mainloop()
My results:

How can i select the display a tkinter file opens on?

I have a tkinter script I want to run, but it goes onto the main monitor on my windows device.
Is there a possible way for me to set which windows display the tkinter windows opens on based off of the display I select in the code?
import tkinter as tk
root=tk.Tk()
def close_program():
root.destroy()
def disable_event():
pass
btn = tk.Button(root, text = "Click me to close", command = close_program)
btn.pack()
root.protocol("WM_DELETE_WINDOW", disable_event)
root.resizable(0,0)
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
root.mainloop()

Python TkInter create a button to open a specific directory

I want to create a button that opens the special directory like "C:\Users\", But as I searched, I did not find any code except the following code:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
gui = Tk()
gui.geometry("100x100")
def getFolderPath():
filedialog.askdirectory()
btnFind = ttk.Button(gui, text="Open Folder",command=getFolderPath)
btnFind.grid(row=0,column=2)
gui.mainloop()
What is the correct way to solve this problem?
Use os.startfile("C:\\Users") to use the system file explorer to open the directory.

How do I create an Import File Button with Tkinter?

So you know how when you use Notepad (on Windows), for example, and you want to open an old file? You click file, then open, then a file dialog opens ups and you can select the file you want, and the program will display its contents.
Basically, I want to make a button in Python that can do that exact thing.
Here's my function for the button-
def UploadAction():
#What to do when the Upload button is pressed
from tkinter import filedialog
When I click on the button assigned to this action, nothing happens, no errors, no crash, just nothing.
import tkinter as tk
from tkinter import filedialog
def UploadAction(event=None):
filename = filedialog.askopenfilename()
print('Selected:', filename)
root = tk.Tk()
button = tk.Button(root, text='Open', command=UploadAction)
button.pack()
root.mainloop()

Categories

Resources