I have a program that does automation by logging to the devices using netmiko library. Tkinter is the front end to provide details like Device getting connected to Username and password. If I keep all the things in a single program it works.
Now I want tkinter GUI program to call program separately using import function. By doing so I want to pass values retried from GUI frontend to backend program to do some function. Where I got stuck is some values that need to pass are inside the backend function collected from frontend GUI.
Seems after import the program its not working, any help would be appreciated.
e1 e2 and e3 values are not passing under show_entry_details function created in separate python file which i imported.
''' the code is regarding tkinter application which works at the front end to prompt user for some info, which later passes to other program to call some function.'''
# Head of Tkinter application
master = Tk()
master.title("Network Automation")
# configuration for the labels and entry
Label(master, text="Device : ").grid(row=0)
Label(master, text="User ID : ").grid(row=1)
Label(master, text="Password : ").grid(row=2)
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master, show='*')
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1, )
# configuration for the button
Button(master, text='Quit', command=master.destroy).grid(row=4, column=0, sticky=W, pady=4)
Button(master, text='Harden', command=show_entry_fields).grid(row=4, column=1, sticky=W, pady=4)
mainloop()
To pass arguments to a button's command, you can use a lambda expression:
button = Button(master,
text='Harden',
command=lambda: show_entry_fields(e1.get(), e2.get(), e3.get()))
button.grid(row=4, column=1)
Related
I'm sure this is going to amount to my misunderstanding of what I'm calling. So I'm trying to make edits to a second window but I don't know that I'm doing it right as it doesn't appear to change. Under def open_win() I created a second window registration(which is supposed to be the equivalent of root). I got the second window to take the Screen position/size but for some reason it wont add the label/entry
from tkinter import *
from functools import partial
#outputs to IDLE
def validateLogin(username, password):
print("username entered :", username.get())
print("password entered :", password.get())
return
#centering Registration page
def open_win():
registration=Toplevel(root)
registration.title("Registration Page")
window_width=600
window_height=400
screen_width =registration.winfo_screenwidth()
screen_height =registration.winfo_screenheight()
center_x=int(screen_width/2-window_width/2)
center_y=int(screen_height/2-window_height/2)
registration.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
#registration label and text entry box
usernameLabel=Label(registration, text="User Name").grid(row=0, column=1)
username=StringVar()
usernameEntry=Entry(registration, textvariable =UserName).grid(row=0, column=2)
#Root Window
root=Tk()
root.title('Sign in Page')
#centering window
window_width=600
window_height=400
screen_width =root.winfo_screenwidth()
screen_height =root.winfo_screenheight()
center_x=int(screen_width/2-window_width/2)
center_y=int(screen_height/2-window_height/2)
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
#username label and text entry box
usernameLabel=Label(root, text="User Name").grid(row=0, column=1)
username=StringVar()
usernameEntry=Entry(root, textvariable=username).grid(row=0, column=2)
#password label and password entry box
passwordLabel=Label(root,text="Password").grid(row=1, column=1)
password=StringVar()
passwordEntry=Entry(root, textvariable=password, show='*').grid(row=1, column=2)
validateLogin=partial(validateLogin, username, password)
#login button
loginButton=Button(root, text="Login", command=validateLogin).grid(row=4, column=1)
SignUpButton=Button(root, text="Sign up", command=open_win).grid(row=4, column=2)
#registration label and text entry box
usernameLabel=Label(registration, text="User Name").grid(row=0, column=1)
username=StringVar()
usernameEntry=Entry(registration, textvariable =UserName).grid(row=0, column=2)
root.mainloop()
Your main issue with not being able to work with the 2nd window is basically a issue of namespace. Your registration variable is stored in the local namespace of the function. If you want to edit it from outside the function like you attempt to do then you need your variable to be in the global namespace.
Because you appear to try and write the same label and entry field a couple of times to the registration top window then I suspect you do not actually need to edit it from outside the function but need to edit it when you created the window.
I have cleaned up your code a little and condensed it to make it a little easier to read.
You should first import tkinter ask tk instead of importing *. This will help prevent any issue with overwriting imports down the road and it makes it a little easier to ID what is referencing a tk widget or some other function.
You use 2 different naming conventions in your code. Chose one and stick with that. It will improve readability. I recommend following PEP8 guidelines.
Items that are not going to be changed later do not need to have variables assigned to them so you can clean up your code a bit there also.
You do not need to go the extra mile to use StringVar here. We can simply pull directly from the entry field as long as the geometry manager (ie grid()) is assigned on a new line so you can still access the variable reference for the entry field.
I am not sure what you were needing partial() for and I think you should use lambda instead in this situation.
If you have any questions let me know.
import tkinter as tk
def validate_login(username, password):
print("username entered :", username.get())
print("password entered :", password.get())
def open_win():
reg = tk.Toplevel(root)
reg.title("Registration Page")
reg.geometry(f'600x400+{int(reg.winfo_screenwidth()/2-600/2)}+{int(reg.winfo_screenheight()/2-400/2)}')
tk.Label(reg, text="User Name").grid(row=0, column=1)
r_un = tk.Entry(reg)
r_un.grid(row=0, column=2)
root = tk.Tk()
root.title('Sign in Page')
root.geometry(f'600x400+{int(root.winfo_screenwidth()/2-600/2)}+{int(root.winfo_screenheight()/2-400/2)}')
tk.Label(root, text="User Name").grid(row=0, column=1)
un = tk.Entry(root)
un.grid(row=0, column=2)
tk.Label(root, text="Password").grid(row=1, column=1)
pw = tk.Entry(root, show='*')
pw.grid(row=1, column=2)
tk.Button(root, text="Login", command=lambda u=un, p=pw: validate_login(u, p)).grid(row=4, column=1)
tk.Button(root, text="Sign up", command=open_win).grid(row=4, column=2)
root.mainloop()
I'm trying to make a quiz app and I have written the following code which is incomplete I am trying to get an output message from the app which gives the answer which the student has written it seems funny but i will do some more stuff on it too. the output I want should be something like this:
the client enters 12
the app shows another box which says your answer is 12
but in this example it is being done for a single answer and it should be performable for more questions too.
import tkinter as tk
from tkinter import *
import math
import random
window=tk.Tk()
def question():
window=tk.Tk()
q1=tk.Label(window, text="enter your question").grid(row=1, column=1)
e1=tk.Entry(window, text="the number of your answer ").grid(row=2, column=1)
b1=tk.Button(window, text="exit", command=window.destroy).grid(row=3, column=1)
window.mainloop()
after your helps I have changed my code to this but still have prolems getting a message box or something like that. the updated code is as follows. now the problem is that as soon as I run the code the empty message box opens and does not wait for me to enter some value into the entry
def question():
window=tk.Tk()
q1=tk.Label(window, text="enter your question")
e1=tk.Entry(window, text="the number of your answer ")
b1=tk.Button(window, text="exit", command=window.destroy)
q1.grid(row=1, column=1)
e1.grid(row=2, column=1)
b1.grid(row=3, column=1)
e1_num=e1.get()
while e1_num==None:
pass
else:
messagebox.showinfo(e1_num)
mainloop()
You could use e1.insert('0', 'subject') to insert something into the entry. You can also replace 'subject' with a variable. To delete the contents of the entry you can use e1.delete(0,END)
You can get the information from the Entry (e1) and store in it a variable like so: e1_num = e1.get()
I hope this helps you :D
Solution to your problem:
from tkinter import *
import tkinter as tk
def question():
window=tk.Tk()
window.geometry('200x125')
window.title('Test')
q1=tk.Label(window, text="Enter your question")
e1=tk.Entry(window)
e2=tk.Entry(window)
b1=tk.Button(window, text="Exit", command=window.destroy)
def Enter():
e1_num = e1.get()
e2.insert('0', e1_num)
q1.pack()
e1.pack()
b2=Button(window, text='Enter', command=Enter)
b2.pack()
e2.pack()
b1.pack()
window.mainloop()
question()
What it will look like:
i am building a small project which involves 4 python files which have their separate functionalities. There is however, a main.py file which uses all the others by importing them.
Now, i have to build a GUI for this project, which i am building within the main.py file. My problem is that some of the other files have functions which print on the console, when the whole project is run, i want those functions to print on the GUI instead. So, how do i print the text from the other file in a Text field created in the main file.
main.py
import second as s
from tkinter import *
def a():
field.insert(END, "Hello this is main!")
root = Tk()
field = Text(root, width=70, height=5, bd=5, relief=FLAT)
button1 = Button(root, width=20, text='Button-1', command=a)
button2 = Button(root, width=20, text='Button-2', command=s.go)
root.mainloop()
second.py
def go():
print("I want this to be printed on the GUI!")
#... and a bunch of other functions...
I just want that when user presses the button-2, then the function go() prints the text on the field
I consider it the best way to try add field as an argument of function go.
Here is my code:
main.py
import second as s
from tkinter import *
def a(field):
field.insert(END, "Hello this is main!\n")
root = Tk()
field = Text(root, width=70, height=5, bd=5, relief=FLAT)
button1 = Button(root, width=20, text='Button-1', command=lambda : a(field))
button2 = Button(root, width=20, text='Button-2', command=lambda : s.go(field))
#to display widgets with pack
field.pack()
button1.pack()
button2.pack()
root.mainloop()
second.py
from tkinter import *
def go(field):
field.insert(END, "I want this to be printed on the GUI!\n")
print("I want this to be printed on the GUI!")
#something you want
The screenshot of effect of running:)
My code is below. It is a very simple UI for logging into my program. The program has multiple TopLevel() instances branching from it, excluded for relevance and brevity. My issue is that once the user logs in, and a top-level instance appears, the main window(below) stays open in the background. Running both self.quit() and self.destroy() methods in the functions of the topLevel instances terminate the entire program instead of simply closing the main window. I believe it is due to how I declared my class but I do not know how to fix it. Any help would be greatly appreciated.
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.KTitle = tk.Label(self, text="Login ")
self.KTitle.grid(row=2,column=0, sticky=E)
self.KUsername = tk.Label(self, text="Username: ")
self.KUsername.grid(row=3,column=0, sticky=E)
self.KPassword = tk.Label(self, text="Password: ")
self.KPassword.grid(row=4,column=0, sticky=E)
self.KUEntry = tk.Entry(self, width=15)
self.KUEntry.grid(row=3,column=1, sticky=W)
self.KUPass = tk.Entry(self, show = '*', width=15)
self.KUPass.grid(row=4,column=1, sticky=W)
Please forgive me that I am relatively new/noob to Python.
Currently I have a test.txt file that my project reads. It is basically in a list.
I want to be able to create a GUI that enables user to enter the information and save it to that test.txt. I also want to be able to click on the "Show" button and displays the current content of test.txt in my GUI.
Update:
Here is my code so far. The save function will save the tkinter entry to my txt file. That has worked fine. My problem is with the "Show" button. I cant use the file.readlines() and get a string of data and put them back to my tkinter GUI. They don't seem to insert into my GUI and I am getting errors ValueError: I/O operation on closed file
from Tkinter import *
def save():
text = e1.get() + "\n"+e2.get() + "\n"+e3.get()
with open("test.txt", "w") as f:
f.writelines(text)
def show():
with open("test.txt", "r") as f:
f.readlines()
e1.get(f.seek(0))
e2.get(f.seek(1))
e3.get(f.seek(2))
master = Tk(className = "ABM Inputs")
Label(master, text="RNG Seed").grid(row=0)
Label(master, text="Manipulator Exists 1=yes, 0=no").grid(row=1)
Label(master, text="Number of Investors").grid(row=2)
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
Button(master, text='Save', command=save).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Show', command=show).grid(row=3, column=1, sticky=W, pady=4)
mainloop( )
What you want is basically a text editor. Have a look in here. The try to use the functions supplied to build your app. I imagine you know about creating buttons and basic tkinter.