I am working on tkinter project. While double clicking the python file, the py.exe file is visible in the background which doesn't look good. so is there a way to make the py.exe invisible or resize it.Please help me with your ideas. Thank you
Sample code:
import tkinter.messagebox
from tkinter import ttk
class Demo1:
data = []
def __init__(self, master):
self.master = master
self.label=tkinter.Label(text="Add IP/Hostname")
self.label.pack()
self.t=tkinter.Text(self.master,height=20,width=50)
self.t.pack()
self.button = tkinter.Button(self.master,height=3,width=10, text="OK"
)
self.button.pack()
def main():
root = tkinter.Tk()
app = Demo1(root)
root.mainloop()
if __name__ == '__main__':
main()
Change the .py extension to .pyw. That should suppress the console window.
Note: the .pyw extension should be associated to be opened with pythonw.exe. Normally this is done by the Python installer. If not, select pythonw.exe the first time you click on a .pyw file. That will make the proper association.
Does that work?
import ctypes
ctypes.windll.user64.ShowWindow(ctypes.windll.kernel64.GetConsoleWindow(), 6 )
Otherwise please post an example code.
Related
I try to make a download interface via tkinter. The following code works well in Pycharm. But when I try to make it an exe file using pyinstaller, it opens a new window every time I click on button1(the download button), which is undesired. Could you please tell me how to fix it? I'm working on Win10. Thanks in advance.
import tkinter as tk
from download import download_yv as dyv
import multiprocessing
class DownloadPage(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.grid()
self.download_flag=True
self.current_row=0
self.current_rowspan=0
self.label1=tk.Label(self,text='Url:',)
self.label1.grid(row=self.current_row,column=0,)
self.text1=tk.Entry(self,width=45)
self.text1.grid(row=self.current_row,column=1,padx=0,pady=0)
self.current_row+=1
self.label2=tk.Label(self,text='Save_Dir:',wraplength=200,)
self.label2.grid(row=self.current_row,column=0)
self.text2=tk.Entry(self,width=45)
self.text2.grid(row=self.current_row,column=1,padx=0,pady=0,)
self.current_row+=1
self.label3=tk.Label(self,text='Overwrite:',)
self.label3.grid(row=self.current_row,column=0)
self.var1=tk.BooleanVar(self)
self.var1.set('False')
self.menu1=tk.OptionMenu(self,self.var1,'True','False')
self.current_rowspan=2
self.menu1.grid(row=self.current_row,column=1,columnspan=2,rowspan=self.current_rowspan,sticky='news')
self.current_row+=self.current_rowspan
self.label4=tk.Label(self,text='Categorize:')
self.label4.grid(row=self.current_row,column=0)
self.var2 = tk.BooleanVar(self)
self.var2.set(True)
self.menu2=tk.OptionMenu(self,self.var2,'True','False')
self.menu2.grid(row=self.current_row,column=1,columnspan=2,rowspan=self.current_rowspan,sticky='news')
self.current_row+=self.current_rowspan
self.current_rowspan=2
self.button1=tk.Button(self,text='download',command=self.download)
self.button1.grid(row=self.current_row,column=1,columnspan=2,rowspan=self.current_rowspan,sticky='news')
self.current_row+=self.current_rowspan
self.button2=tk.Button(self,text='Stop Downloading',command=self.stop_download)
self.button2.grid(row=self.current_row,column=1,columnspan=1,)
def download(self):
var1=self.var1.get()
var2=self.var2.get()
# print(var1,type(var1),var2,type(var2))
self.download_process=multiprocessing.Process(target=dyv,args=(self.text1.get(),self.text2.get(),var1,var2))
self.download_process.start()
def stop_download(self):
if self.download_process and self.download_process.is_alive():
self.download_process.terminate()
if __name__ == '__main__':
root=tk.Tk()
app=DownloadPage(root)
app.mainloop()
Not sure how to change the icon of a simple dialog window. I try to use .bitmap but doesn't work. Need Help
Just add the default keyword argument to the iconbitmap() for the icon of the root, then all the child windows will inherit the icon.
import tkinter
from tkinter import simpledialog
root = tkinter.Tk()
root.iconbitmap(default="C:\\Users\\username\\random.ico")
dialog = simpledialog.askstring("INFO", "wut ur name?")
root.mainloop()
Result with default:
Results without default:
Are you looking for this
from tkinter import Tk
master = Tk()
photo = PhotoImage(file = "Any image file")
master.iconphoto(False, photo)
I decided I want to learn how to make GUIs with something more than entry fields and buttons, so I'm starting off with tabs. After a little bit of research I made myself a program, but don't know why it doesn't work.
# --IMPORTS--
from tkinter import *
import tkinter.ttk as ttk
import time
# --CLASSES--
class Gui:
def __init__(self):
self.root = Tk()
self.root.title("tab test")
def setup(self):
# tabs
tabc = ttk.Notebook(self.root)
tab1 = ttk.Frame(tabc)
tabc.add(tab1, text="test 1")
tabc.grid()
def run(self):
self.root.mainloop()
# --MAIN--
if __name__ == "__main__":
gui = Gui()
gui.run()
When I run the program I just get a blank screen (see screenshot) and there is no way to see if there is a tab, let alone which one is selected.
Like I said, I don't see the reason why it isn't working. There are no error messages to point me in the right direction and I'm not 100% sure on how tabs work to begin with, so I thought I'd ask here. I've tried changing .grid() to .pack() but I think it's more of an error on my end than a bug with tkinter. Thanks in advance!
you have to run your setup method.
# --MAIN--
if __name__ == "__main__":
gui = Gui()
gui.setup()
gui.run()
So when I am calling the list directory code below directly then it's working fine. But when I am calling main.py which uses app class from gui which calls the list directory function, It prints "yoo" but prints an empty list instead of list of directories. I am stuck and can't figure out why that's happening. Any ideas?
Outputs:
When list directory called directly :
["/home/shubham/Desktop/movies/djangounchained.mkv"]
"yoo"
When called by main.py with same argument:
[]
"yoo"
Here is my main script
from gui import app
from list_directory import display_files
import tkinter as tk
root = tk.Tk()
directory = input("Enter directory name:")
root.geometry("400x300")
widgets_creator = app(root)
name = "get list"
directory_button = widgets_creator.create_button(name,function=display_files,path=directory)
root.mainloop()
Here is my gui script
import tkinter as tk
class app(tk.Frame):
def __init__(self,master):
super(app,self).__init__(master=master)
self.master = master
self.init_window()
def init_window(self):
# changing the title of our master widget
self.master.title("GUI")
# allowing the widget to take the full space of the root window
self.pack(fill=tk.BOTH, expand=1)
# creating a button instance
quitButton = tk.Button(self, text="Quit")
# placing the button on my window
quitButton.place(x=0, y=0)
def create_button(self,button_name,function,path):
button = tk.Button(self.master,text=button_name,command=lambda: function(path))
button.place(x=200,y=5)
return button
Here is my list_directory code:
import glob
def display_files(path):
x = glob.glob(path)
print(x)
print("yoo")
if __name__ == '__main__':
display_files("/home/shubham/Desktop/movies/*")
I might have found your problem. The code works fine, the problem is your argument. For example, if I enter '/Users/rudy/Desktop/*' when the input prompt comes up, I have the same result as you.
However, when I enter /Users/rudy/Desktop/* (without quotes), everything works fine. input() already saves the input as a string, so you don't need to add additional quotes.
I'm using jupyter notebook on mac, recently I need to write interactive dialog box, so after google, I use Tkinter to make an interactive window.
But I was bothered by this problem couples day ,and still can't find a solution way.
Fisrt example:
from Tkinter import *
from tkFileDialog import askopenfilename
import sys
import os,time
def callback():
name= askopenfilename()
print name
errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop()
Second example:
from Tkinter import *
import sys,os
class YourApp(Tk):
def quit_and_close(self):
app.quit()
#os._exit(0)
#sys.exit(1)
#exit(0)
app = YourApp()
app.title('example')
app.geometry('400x300+200+200')
b = Button(app, text = "quit", command = app.quit_and_close)
b.pack()
app.mainloop()
And the third one:
import Tkinter as tk
import tkMessageBox
def ask_quit():
if tkMessageBox.askokcancel("Quit", "You want to quit now? *sniff*"):
root.destroy()
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", ask_quit)
root.mainloop()
After running those above code, always need have to force quit python launcher.
It is very weird, and annoying because after forcing quit, I will got the error:
Is it necessary to use python launcher as default window?
Is there possible to set another window to open ?
or is there proper way to close the launcher without causing programming crash?
p.s Even I try to use wxpython, it still open python launcher and got the same problem.