I want to make a benchmark app in python using Tkinter - python

I want it to show the user input but it is not showing. Please send help.
I have tried code from multiple posts but no luck.
I was able to make it show the 'selected' option but for no avail. Please help me
import tkinter as tk
from tkinter.ttk import *
app = tk.Tk()
# Functions and scripts
def OnSubmit():
print(str(usrnme.get()))
print(str(passw.get()))
print(str(selected.get()))
app.title("Pymark: Landing Page")
app.geometry("600x600")
app.resizable(0, 0)
desc = """
PyMark is the first benchmark created using python's native GUI creator "Tkinter" and using hashes to rank performance.
The sofware is light and can run on older CPU's like i7-4510U (the processor I use) and does not require a lot of memory.
I am still open for constructive critisism and community feedback on my work as I am a beginner programmer.
IMPORTANT: PyMark can still experience bugs thus, pull requests are encouraged!
Please continue enjoying PyMark!
"""
# Landing Page
lpframe = Frame(app)
mainheading = Label(lpframe, text="Welcome to PyMark!", font = ('Times New Roman', 20))
maindesc = Label(lpframe, text=f'{desc}', font = ('Times New Roman', 9))
lpframe.pack()
mainheading.pack(padx = 10, pady = 10)
maindesc.pack(padx = 10, pady = 10)
# Login Page
loginframe = Frame(app)
headerframe = Frame(loginframe, relief = 'groove')
loginheader = Label(headerframe, text='Login / Register: ', font = ('Times New Roman', 12))
usrnmelb = Label(loginframe, text = 'Username: ', font = ('Times New Roman', 9))
passlb = Label(loginframe, text = 'Password: ', font = ('Times New Roman', 9))
selected = tk.StringVar()
register = Radiobutton(loginframe, text='Register', value='reg', variable=selected)
login = Radiobutton(loginframe, text='Login', value='log', variable=selected)
submitbtn = Button(loginframe, text = 'Submit', command=OnSubmit)
usrnmeentr = Entry(loginframe)
passentr = Entry(loginframe)
loginframe.pack()
headerframe.pack(padx = 20, pady = 20)
loginheader.pack(padx = 20, pady = 20)
usrnmelb.pack()
usrnmeentr.pack()
passlb.pack()
passentr.pack()
register.pack()
login.pack()
submitbtn.pack(padx = 10, pady = 10)
usrnme = tk.StringVar()
passw = tk.StringVar()
usrnmeentr.focus()
app.mainloop()
I expected it to return the username, password and the selected option but it only told the selected option and nothing else.

The OnSubmit() function does not get the correct inputs to print.
Get the entryfields content with the method .get().
In your case the OnSubmit() function should look like this:
def OnSubmit():
print(str(usrnmeentr.get())) #usrnme.get() was not the name of the entry box
print(str(passentr.get()))
print(str(selected.get()))

You must link your Entry to Stringvar(), not your radio buttons
import tkinter as tk
app = tk.Tk()
app.geometry('300x300')
# Functions and scripts
def OnSubmit():
print(var.get())
var = tk.StringVar()
entry = tk.Entry(app, textvariable=var)
entry.pack()
bt = tk.Button(app, text='Show', command=OnSubmit)
bt.pack()
app.mainloop()

Related

I want to get a value from another window?(python, tkinter)

I wanted to get a value from another window so i saved the value using xlsxwriter and got it using pandas but the problem is that i get the value not after the that window is destroyed but after the main window is closed?
can you tell me how i get the value without closing the main window??
Here is a helpful link with a simple solution to what you are attempting to accomplish. Basically, you need one frame to accept a variable as input and the other frame to 'get' the variable from the first frame.
https://www.code4example.com/python/tkinter/tkinter-passing-variables-between-windows/
** copied here from source for simplicity **
from tkinter import *
class Frames(object):
def newFrame(self):
newwin = Toplevel(root)
newwin.title('New Window')
newwin.geometry("200x100")
newwin.resizable(0, 0)
#getting parameter via query var
display = Label(newwin, text="Hello, " + self.query.get())
display.pack()
def mainFrame(self,root):
self.query = StringVar() #passing parameter via query var
root.title('Open Window!!!')
root.geometry("200x200")
root.resizable(0, 0)
button1 =Button(root, text ="Open and Send New Window", command =self.newFrame)
button1.place(x = 15, y = 25, width=170, height=25)
entry1 = Entry(root, textvariable=self.query)
entry1.place(x = 50, y = 75, width=100, height=25)
root = Tk()
app = Frames()
app.mainFrame(root)
root.mainloop()

no attribute to destroy

I have two functions the top_signup_click and the top_login_click. The top_signup_click function is below the top_login_click function. The top_login_click function doesn't work because the_top_signup_click function is below it. To fix that issue I'll have to put the top_signup_click above the top_login_click function, which does solve the issue but it also creates another issue, now the top_signup_click function doesn't work. For both functions to work they both have to be below each other, but I don't think that's possible. If you know any other ways to do this please help. I tried using global functions to fix it but it didn't work.
import tkinter as tk
import tkinter.font as font
import tkinter.messagebox
signup_button = None
root = tk.Tk()
root.geometry('360x460')
#login stuff
def login_click():
readfile = open("emails.txt", "r")
lines = readfile.readlines()
isIN = False
for line in lines:
if f'{entry.get()}, {entry1.get()}' in line:
isIN = True
if not isIN:
tk.messagebox.showinfo(message ="You got your email/password wrong, are you sure you signed in?")
else:
tk.messagebox.showinfo(message ="You hacker, the email/password is correct")
def signup_click():
file = open("emails.txt","a")
file.write (entry.get())
file.write(f', {entry1.get()}\n')
def top_login_click():
global signup_button
signup_button.destroy()
login_button = tk.Button(root, text="Login", bg='royalblue', fg='white', command = login_click, width = 15, height = 2)
login_button['font'] = button_font
login_button.place(x=88,y=330)
#when the top sign up button is pressed
def top_signup_click():
global login_button
login_button.destroy()
signup_button = tk.Button(root, text="Sign-Up", bg='royalblue', fg='white', command = login_click, width = 15, height = 2)
signup_button['font'] = button_font
signup_button.place(x=88,y=330)
top_font = font.Font(family='Helvetica', size=14, weight='bold')
label_font = font.Font(family='Helvetica', size=20)
button_font = font.Font(family='Helvetica', size=14, weight='bold')
top_login_button = tk.Button(root,text = 'Login',width = 15, height = 3, command = top_login_click)
top_login_button.place(x=0,y=0)
top_login_button['font'] = top_font
top_signup_button = tk.Button(root,text = 'Sign-Up',width = 15, height = 3, command = top_signup_click)
top_signup_button.place(x=180,y=0)
top_signup_button['font'] = top_font
username_label = tk.Label(root, text = 'Username: ' )
username_label['font'] =label_font
username_label.place(x=120,y=120)
entry = tk.Entry(root, width = 40)
entry.place(x=65,y=170, height = 30)
password_label = tk.Label(root, text = 'Password: ' )
password_label['font'] =label_font
password_label.place(x=120,y=230)
entry1 = tk.Entry(root, width = 40)
entry1.place(x=65,y=280, height = 30)
login_button = tk.Button(root, text="Login", bg='royalblue', fg='white', command = login_click, width = 15, height = 2)
login_button['font'] = button_font
login_button.place(x=88,y=330)
root.mainloop()
The top_login_click function is on line 29-34 and the top_signup_click function is on line 37-42. Thanks in advance.
I wanted to comment this but seems like I don't have enough reputation but here is what I think.
You have set the variable signup_button = None right below the import statements so signup_button is not really a tk.Button object, instead it's a NoneType variable.
So just remove that statement and create
signup_button = tk.Button(<args here>)
and You should be good.
and try using place_forget() instead of destroy() IFF destroy doesn't work for you.
Edit:
What I also think in your case that you may not really need to destroy the signup_up button because you haven't created one yet. Think about it this way (based on the GUI that comes up on running your code) - on the landing screen of your app (the first screen that comes up), you have a login form with two entry boxes for email & pword and a login button, along with two more buttons called top_login_button and top_signup_button. So You DO NOT have a signup_button as of now (and according to your code signup_button = None it's not a button). So if a user now clicks on the top_login_button, the statement signup_button.destroy() will look for widget with the name signup_button (which is set to None by you) so it raises the error.
You code will run fine in a case where user clicks on top_signup_button before clicking on top_login_button because in that case the signup_button will be actually set to be a tk.Button object.

I am having trouble getting outputs from my python form

I am having trouble gaining the outputs from this form and i cant seem to identify where its going wrong.
from tkinter import *
def Button_to_text():
firstname_info = firstname.get()
lastname_info = lastname.get()
age_info = age.get()
print(firstname_info,lastname_info,age_info)
screen = Tk()
screen.geometry("500x500")
screen.title("python_form")
heading = Label(text = "Demo Form",bg = "orange", fg="black",width = "500")
heading.pack()
firstname_text = Label(text="firstname")
lastname_text = Label(text="lastname")
age_text = Label(text="age")
firstname_text.place(x=60, y= 40)
lastname_text.place(x=60,y=80)
age_text.place(x=60,y=120)
firstname = StringVar()
lastname = StringVar()
age = IntVar()
firstname_entry = Entry(textvariable = firstname)
lastname_entry = Entry(textvariable = lastname)
age_entry = Entry(textvariable = age)
firstname_entry.place(x=160, y=40)
lastname_entry.place(x=160,y=80)
age_entry.place(x=160,y=120)
register = Button(text = "register", width= "30",height ="2", command = Button_to_text())
register.place(x=50,y=290)
I followed a tutorial and my computing teacher cant help because he doesn't know python. plus my friends cant seem to identify the issue there are also no errors coming up so I know its a logic error and i also cant work out how to step so i can check variables
thanks to anyone who can help.
There are two problems with your code:
You have to use a mainloop to keep the window being displayed continuously.
You should not use the parentheses() while passing any function to a Button as an argument.
Note: And if the function has its own parameters then you will have to use lambda while passing it to a Button. But in your case, you can simply remove the parentheses().
Here's the fixed code:
from tkinter import *
def Button_to_text():
firstname_info = firstname.get()
lastname_info = lastname.get()
age_info = age.get()
print(firstname_info, lastname_info, age_info)
screen = Tk()
screen.geometry("500x500")
screen.title("python_form")
heading = Label(text="Demo Form", bg="orange", fg="black", width="500")
heading.pack()
firstname_text = Label(text="firstname")
lastname_text = Label(text="lastname")
age_text = Label(text="age")
firstname_text.place(x=60, y=40)
lastname_text.place(x=60, y=80)
age_text.place(x=60, y=120)
firstname = StringVar()
lastname = StringVar()
age = IntVar()
firstname_entry = Entry(textvariable=firstname)
lastname_entry = Entry(textvariable=lastname)
age_entry = Entry(textvariable=age)
firstname_entry.place(x=160, y=40)
lastname_entry.place(x=160, y=80)
age_entry.place(x=160, y=120)
register = Button(text="register", width="30", height="2", command=Button_to_text)
register.place(x=50, y=290)
screen.mainloop()
Note:
As a good practice, you should always use small letters in the names of functions like this: def button_to_text():.
And you should always import tkinter as tk instead of importing all * from tkinter. It is always a good practice. The only change you will need to do in the program is that you will need to use tk. before each item belonging to tkinter. Like this: screen = tk.Tk()

How to allow a tkinter window to be opened multiple times

I am making a makeshift sign in system with python. Currently if you enter the correct password it brings up a new admin window. If you enter the wrong one it brings up a new window that says wrong password. If you exit out of one of those windows and then try to enter a password again it breaks. tkinter.TclError: can't invoke "wm" command: application has been destroyed Is there a way to prevent this so if someone enters the wrong password they don't need to restart the app?
import tkinter
from tkinter import *
#define root window
root = tkinter.Tk()
root.minsize(width=800, height = 600)
root.maxsize(width=800, height = 600)
#define admin window
admin = tkinter.Tk()
admin.minsize(width=800, height = 600)
admin.maxsize(width=800, height = 600)
admin.withdraw()
#define wrong window
wrong = tkinter.Tk()
wrong.minsize(width=200, height = 100)
wrong.maxsize(width=200, height = 100)
wrong.withdraw()
Label(wrong, text="Sorry that password is incorrect!", font=("Arial", 24), anchor=W, wraplength=180, fg="red").pack()
#Admin sign in Label
areAdmin = Label(root, text="Administrator sign in", font=("Arial", 18))
areAdmin.pack()
#password label and password
passwordLabel = Label(root, text="Password: ", font=("Arial", 12))
passwordLabel.place(x=300, y=30)
#password entry
adminPasswordEntry = Entry(root)
adminPasswordEntry.place(x=385, y=32.5)
#function for button
def getEnteredPassword():
enteredPassword = adminPasswordEntry.get()
if enteredPassword == password:
admin.deiconify()
else:
wrong.deiconify()
#enter button for password
passwordEnterButton = Button(root, text="Enter", width=20, command=getEnteredPassword)
passwordEnterButton.place(x=335, y=60)
mainloop()
I don't know tkinter much but I could fix your code, I hope it's a proper fix.
Create Toplevel windows not Tk. those are dialog windows, as opposed to Tk window which must be unique. Same look & feel, same methods
Create windows when needed, and each time. Else, closing them using the close gadget destroys them.
Fixed code, enter wrong or good password as many times you want without crash:
import tkinter
from tkinter import *
password="good"
#define root window
root = tkinter.Tk()
root.minsize(width=800, height = 600)
root.maxsize(width=800, height = 600)
#Admin sign in Label
areAdmin = Label(root, text="Administrator sign in", font=("Arial", 18))
areAdmin.pack()
#password label and password
passwordLabel = Label(root, text="Password: ", font=("Arial", 12))
passwordLabel.place(x=300, y=30)
#password entry
adminPasswordEntry = Entry(root)
adminPasswordEntry.place(x=385, y=32.5)
#function for button
def getEnteredPassword():
enteredPassword = adminPasswordEntry.get()
if enteredPassword == password:
admin = tkinter.Toplevel()
admin.minsize(width=800, height = 600)
admin.maxsize(width=800, height = 600)
#admin.withdraw()
else:
wrong = tkinter.Toplevel()
wrong.minsize(width=200, height = 100)
wrong.maxsize(width=200, height = 100)
Label(wrong, text="Sorry that password is incorrect!", font=("Arial", 24), anchor=W, wraplength=180, fg="red").pack()
#enter button for password
passwordEnterButton = Button(root, text="Enter", width=20, command=getEnteredPassword)
passwordEnterButton.place(x=335, y=60)
mainloop()

python - How to put the input from an Entrybox to a label in Tkinter?

I am trying to put the information submitted into the data entry box into a label of my canvas. So far all I get on the label is this.
PY_VAR#
This is my current code.
import tkinter
from tkinter import *
font = ("Times New Roman", 5)
font2 = ("Times New Roman", 10)
font3 = ("Times New Roman", 15)
def getData():
1a.get()
2a.get()
test()
def enterData():
global 1a, 2a
canvas = tkinter.Canvas(root, width=800, height=600)
box1 = Entry(textvariable = 1a).place(x=100, y=200)
box2 = Entry(textvariable = 2a).place(x=300, y=200)
Button(text = "enter data", font = font2, command = getData).place(x=560, y=100)
def test():
global 1a, 2a
1a = StringVar()
2a = StringVar()
root = tkinter.Tk()
canvas = tkinter.Canvas(root, width=800, height=600)
canvas.pack()
Label(root, text = 1a, font = font3).place(x=70, y=400)
enterData()
I've also tried seeing what 1a and 2a is assigned to, it is always PY_VAR#.
Could anyone see what is wrong with my code?
First of all, you don't want to create new StringVars every time you click the button, so move the creation of those to the top, and rename them - you can't start a variable with a number (don't forget to change the reference to 1a and 2a in the enterData function too):
# import tkinter You shouldn't import tkinter in two different ways - get rid of this one...
from tkinter import *
font = ("Times New Roman", 5)
font2 = ("Times New Roman", 10)
font3 = ("Times New Roman", 15)
stringVar1a = StringVar()
stringVar2a = StringVar()
Second, the 1a.get() and 2a.get() lines do nothing - they get the contents of the entry, but nothing is done with them. So really you don't need a separate getData and test functions, but for the sake of preserving your structure, I'll leave them:
def getData():
test()
Third, when you create the label, you don't want to set its text attribute to the StringVar, you want to set the textvariable attribute to the StringVar:
Label(root, textvariable = stringVar1a, font = font3).place(x=70, y=400)
You're assigning the text parameter of your Label to the variable 1a, not the content. Use this instead:
Label(root, text=1a.get())
But I have to say, this code is a bit ridiculous. 1a and 2a can't even be variables, place() is often enough a bad idea for creating GUIs, you're doing unnecessary operations (the function getData is completely obsolete) and you're always creating new labels.

Categories

Resources