I'm working on a new project in visual studio as shown in the code below, and the GUI using Tkinter is not working in visual studio. This is my first time using visual studio and I can't seem to find why it won't work.
from tkinter import *
import tkinter as ttk
#import os #not needed
root = Tk()
#Setting up the window
root.geometry("250x100")
root.resizable(width=False, height=False)#Disables user to resize window
root.title("Login")
#Temp "DataBase"
users=[("josh","python")] #<<<here ''josh'' is user and ''python'' i5s password
admins=[("josh1","python1")]
# Login and signup function
def login(): #login function
if (t1.get(),t2.get())in users: #Temp for testing
root.destroy()
import MainWindow
# os.system("MainWindow") #does not work
print("welcome")
elif (t1.get(),t2.get())in admins: #Temp for testing
root.destroy()
import AdminMainWindow
# os.system("AdminMainWindow") #does not work
print("welcome Admin")
else:
error.config(text="Invalid username or password")
def signup(): #signup function
root.destroy
import SignupWindow
# os.system("SignupWindow") #does not work
#arranging display varables
top = Frame(root)
bottom = Frame(root)
top.pack(side=TOP, fill=BOTH, expand=True)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
#error placement and font
error = Label(root, font=("blod",10))
error.place(x=40,y=55)
#input display setup
l1 = Label(root,text="Username:")
l2 = Label(root,text="Password:")
t1 = Entry(root, textvariable=StringVar())
t2 = Entry(root, show="*", textvariable=StringVar())
b1 = Button(root,text="Login", command=login)
b2 = Button(root,text="Signup", command=signup)
#organising
l1.pack(in_=top, side=LEFT)
t1.pack(in_=top, side=LEFT)
l2.pack(side=LEFT,)
t2.pack(side=LEFT,)
b1.pack(in_=top, side=BOTTOM)
b2.pack(in_=bottom, side=BOTTOM)
#end of Tk loop
root.mainloop()
It comes up with the python command line and says press any key to continue.
I also looked online and they all say it because people don't end the Tk loop, but I have.
before you make a new project I created a new file and places all the code in there. then add one code to VS at a time then it works but not when you do it all together.
On ms-windows, python programs using tkinter should have the extension .pyw. And this extension should be associated with pythonw.exe rather than python.exe.
Using pythonw.exe will prevent the cmd.exe window from appearing when your python script has a GUI.
Related
I have been trying to execute 2 commands in 1 button. I have read that using lambda can solve the problem. But my situation is a little different. On button press, one command is to destroy the existing GUI, and the second command, I want is to open another SERVER GUI. Below is my existing button functionality.
exit_button = Button(topFrame, text='Quit', command=window.destroy)
How can I open another GUI using same button?
Thank you for any help.
Edit:-
I have now created the below function:
def close_func():
os.kill(os.getpid(), signal.SIGINT)
GUI_Interface()
window.destroy()
server_socket.close()
GUI_Interface is a function that I need to call after closing the existing .py file. If I put GUI_Interface as the first command for close_func(), then it really does not go back to the 2nd step and never closes the existing.py file.
And if I place GUI_Interface in the end, it just closes the existing one and nevr opens the function of the new .py file
Three options at least:
using or (with lambda if arguments):
from tkinter import Tk, Button
root = Tk()
Button(root, text='Press', command=lambda: print('hello') or root.destroy() or print('hi')).pack()
root.mainloop()
important to use or because and didn't work (don't know why or how this works at all)
or use function definitions:
from tkinter import Tk, Button
def func():
print('hello')
root.destroy()
print('hi')
root = Tk()
Button(root, text='Press', command=func).pack()
root.mainloop()
or lists with lambda:
from tkinter import Tk, Button
def func():
print('hello')
root.destroy()
print('hi')
root = Tk()
Button(root, text='Press', command=lambda: [print('hello'), root.destroy()]).pack()
root.mainloop()
hey i am making a program which records desktop screen.
So what i want to do is when ever i click on my start button(tkinter Gui) in my gui window.
It should start a timer like 3.... ,2.... ,1.... in big font directly on my desktop screen and not on my tkinter window. and then my function should start.
How can i do that ..
import tkinter as tk
from tkinter import *
root = Tk()
root.title("our program")
start_cap =tk.button(text='start recording' command=start_capute)
start_cap.pack()
root.mainloop()
Not mentioning the functions and the entire code here as not necessary the code is working fine and i just want to add a new feature of the timer in it.
An minimal example:
import tkinter as tk
# from tkinter import *
def Start():
def Count(Number):
if Number == -1:
win.withdraw()
print("Start") # what you want to do
return False
NumberLabel["text"] = Number
win.after(1000,Count,Number-1)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
win = tk.Toplevel()
win.geometry("+%d+%d"%((screen_width-win.winfo_width())/2,(screen_height-win.winfo_height())/2)) # make it in the center.
win.overrideredirect(1)
win.wm_attributes('-topmost',1) # top window
win.wm_attributes('-transparentcolor',win['bg']) # background transparent.
NumberLabel = tk.Label(win,font=("",40,"bold"),fg='white')
NumberLabel.pack()
win.after(0,lambda :Count(3))
win.mainloop()
root = tk.Tk()
root.title("our program")
start_cap = tk.Button(text='start recording',command=Start)
start_cap.pack()
root.mainloop()
I have written a task reminder application in python and tkinter. It schedules the reminders using the Task Scheduler. The reminders are displayed by a small GUI program in a certain location on the screen. My problem is that the reminders overlap each other. When multiple reminders appear, how can I make them appear in distinct positions? Please note that I am referring to a separate invocation of the GUI program for each reminder.
The situation is similar to opening, say, multiple copies of the calculator program. They open in distinct locations on the screen. How does it happen?
The program that creates the reminders is as follows -
from tkinter import *
import shelve
import sys
def showTask(parent, key):
parent.title('Reminder')
parent.geometry('300x100-0-40')
parent.rowconfigure(0, weight=1)
parent.columnconfigure(0, weight=1)
shelfFile = shelve.open('C:\\Users\\hp\\pypgms\\data\\tasks')
message = shelfFile[key]['name']
shelfFile.close()
Label(parent, text=message).grid(padx=20, pady=20, sticky=NW)
btn = Button(parent, text='Ok', command=parent.quit)
btn.grid(pady=5)
btn.bind('<Return>', lambda e: parent.quit())
key = sys.argv[1]
root = Tk()
showTask(root, key)
root.mainloop()
Tkinter windows have a convenient method called geometry() to define their size and position on the screen following this format:
root.geometry('250x150+300+300') # width=250, height=150, position=(300,300)
See below how you can use it to open reminder windows next to one another:
import Tkinter as tk
class MainApp():
def __init__(self, root):
self.root = root
self.reminderWindows = []
self.button = tk.Button(self.root, text="New reminder",
command=self.open_new_reminder)
self.button.pack()
def open_new_reminder(self):
reminder = tk.Toplevel(self.root)
self.reminderWindows.append(reminder)
windowNumber = len(self.reminderWindows)
reminder.geometry("100x120+{}+200".format(str(150*windowNumber)))
if __name__ == "__main__":
app = tk.Tk()
MainApp(app)
app.mainloop()
this is my code so far o cant add buttons with out it creating more windows
////////
#import tkinter
import tkinter
#import tkmessagebox(buttons)
from tkinter import *
#create a new window
window = tkinter.Tk()
#title <------ put it before .mainloop
window.title("yeahh boiiii")
#window size
window.geometry("500x500")
#set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico')#<---- 8bit file name
master = Tk()
def callback():
print ("click!")
b = Button(master, text="OK", command=callback)
b.pack()
#draws the window
window.mainloop()
////////
please help
Your problem is that you create 2 instances of Tk(). This is a bad idea, and you don't need to do it since you can make your button a child of the window object:
# Import tkinter
import tkinter as tk
# Create a new window
window = tk.Tk()
# Title <------ put it before .mainloop
window.title("yeahh boiiii")
# Window size
window.geometry("500x500")
# Set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico') #<---- 8bit file name
def callback():
print ("click!")
b = tk.Button(window, text="OK", command=callback)
b.pack()
# Draw the window
window.mainloop()
I also rewrote your tkinter import, because you were importing it twice...
i am a newbie # python GUI's so i am using an online tutorial. when i run the following code, i get an error saying 'window' is not defined. i am using a windows PC with windows 7 and python 3.3 installed.
from tkinter import *
window.title("Test Window")
window.geometry('300x300')
window.wm_iconbitmap('Generals.ico') # Generals.ico is a filename for the window icon
lbl = tkinter.Label(window, text='Label')
lbl.pack()
window.mainloop()
your missing one line. You need to add window = Tk()
so your code should look like:
from tkinter import *
window = Tk()
window.title("Test Window")
window.geometry('300x300')
window.wm_iconbitmap('Generals.ico') # Generals.ico is a filename for the window icon
lbl = tkinter.Label(window, text='Label')
lbl.pack()
window.mainloop()
Not sure if this is 100% correct cause I'm using python 2.7. You might not need to capitalive the 'T' in tk()