How to make a window with buttons in python
Asked Answered
G

6

8

How do I create a function that makes a window with two buttons, where each button has a specified string and, if clicked on, returns a specified variable? Similar to @ 3:05 in this video https://www.khanacademy.org/science/computer-science-subject/computer-science/v/writing-a-simple-factorial-program---python-2 (I know it's a tutorial for a very easy beginners program, but it's the only video I could find) but without the text box, and I have more controls over what the 'ok' and 'cancel' buttons do.

Do I have to create a window, draw a rect with a string inside of it, and then make a loop that checks for mouse movement/mouse clicks, and then return something once the mouse coords are inside of one of the buttons, and the mouse is clicked? Or is there a function/set of functions that would make a window with buttons easier? Or a module?

Glebe answered 1/3, 2014 at 1:5 Comment(0)
B
19

Overview

No, you don't have to "draw a rect, then make a loop". What you will have to do is import a GUI toolkit of some sort, and use the methods and objects built-in to that toolkit. Generally speaking, one of those methods will be to run a loop which listens for events and calls functions based on those events. This loop is called an event loop. So, while such a loop must run, you don't have to create the loop.

Caveats

If you're looking to open a window from a prompt such as in the video you linked to, the problem is a little tougher. These toolkits aren't designed to be used in such a manner. Typically, you write a complete GUI-based program where all input and output is done via widgets. It's not impossible, but in my opinion, when learning you should stick to all text or all GUI, and not mix the two.

Example using Tkinter

For example, one such toolkit is tkinter. Tkinter is the toolkit that is built-in to python. Any other toolkit such as wxPython, PyQT, etc will be very similar and works just as well. The advantage to Tkinter is that you probably already have it, and it is a fantastic toolkit for learning GUI programming. It's also fantastic for more advanced programming, though you will find people who disagree with that point. Don't listen to them.

Here's an example in Tkinter. This example works in python 2.x. For python 3.x you'll need to import from tkinter rather than Tkinter.

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        # create a prompt, an input box, an output label,
        # and a button to do the computation
        self.prompt = tk.Label(self, text="Enter a number:", anchor="w")
        self.entry = tk.Entry(self)
        self.submit = tk.Button(self, text="Submit", command = self.calculate)
        self.output = tk.Label(self, text="")

        # lay the widgets out on the screen. 
        self.prompt.pack(side="top", fill="x")
        self.entry.pack(side="top", fill="x", padx=20)
        self.output.pack(side="top", fill="x", expand=True)
        self.submit.pack(side="right")

    def calculate(self):
        # get the value from the input widget, convert
        # it to an int, and do a calculation
        try:
            i = int(self.entry.get())
            result = "%s*2=%s" % (i, i*2)
        except ValueError:
            result = "Please enter digits only"

        # set the output widget to have our result
        self.output.configure(text=result)

# if this is run as a program (versus being imported),
# create a root window and an instance of our example,
# then start the event loop

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()
Byre answered 2/3, 2014 at 13:52 Comment(5)
Great example. Could you please explain what are fill="both", fill="x", padx=20 and anchor="w" doing.Lonnylonslesaunier
@AlexanderCska: you can get the answers to those questions by reading the tkinter documentation. A good place to start is effbot.org/tkinterbook/pack.htmByre
@BryanOakley, it looks like Tkinter was replaced with Tk and Frame to frame, you think you want to update your answer to make it up to dateQuoits
@TechGeek: Frame hasn't changed, but you're correct that Tkinter was renamed tkinter in python 3. I addressed that in the answer.Byre
@BryanOakley, OkayQuoits
S
1

You should take a look at wxpython, a GUI library that is quite easy to start with if you have some python knowledge.

The following code will create a window for you (source):

import wx

app = wx.App(False)  # Create a new app, don't redirect stdout/stderr to a window.
frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.
frame.Show(True)     # Show the frame.
app.MainLoop()

Take a look at this section (how to create buttons). But start with the installation instructions.

Saturninasaturnine answered 1/3, 2014 at 12:52 Comment(0)
O
1

Here's my part. I'm making an AI chatbot, just working on the interface of logging in and sketching out stuff. I'm also a beginner in .json so this helped me learn.

I'll also explain it maybe.

First, create a .json file named anything you'd like. Make sure the file is in the same directory path/folder as the code, or you can import os to do that.

Next import Tkinter. It may or may not be an in-built module but try it and see. Also import JSON, time isn't required but can help.

import tkinter as tk
import json
import time

Next, create a window for all this to happen. Edit the info if necessary.

app = tk.Tk()
app.wm_title("Sign Up or Log in")
app.geometry("500x300")

Add a label if you want.

k = tk.Label(text = "Hello there, I'm Lola! We'll talk soon. Just let me know your credentials!\nJust click on sign up below so that I can identify you!", justify="left")
k.pack()

Add a button for the user to click.

sign_in = tk.Button(text="Sign Up or Log In", command=signin)
sign_in.pack()

We need to define the signing function used above for the button. So before we create the button, we define it. It's a bit long so I'll just explain the general parts. We first get their details before we check it

def signin():
em = tk.Label(text="Email:")
em.pack()
en1 = tk.Entry(width=50)
en1.pack()
pa = tk.Label(text="Password:")
pa.pack()
en2 = tk.Entry(width=50)
en2.pack()
na = tk.Label(text="Name:")
na.pack()
en3 = tk.Entry(width=50)
en3.pack()

Next, let's define the submit function and create the button. This is where json comes in. We first get the details and store them in a variable like so.

def submit():
    email = str(en1.get())
    password = str(en2.get())
    name = str(en3.get())
    login = tk.Label(text="")

Then, we shouldn't forget to read the json file first

with open("info.json", "r") as f:
        users = json.load(f)

Now let's do the checking

if email in users:
        login.config(text="You already have an account! Click log in please!")
        loginn = tk.Button(text = "Log in", command = login)
    else:
        users[email] = {}
        users[email]["Password"] = password
        users[email]["Name"] = name
        with open("info.json", "w") as f:
            json.dump(users, f)
        login.config(text=f"You've successfully created an account. Just click on log in below! Credentials:\nEmail: {email}\nPassword: {password}\nName: {name}")
    login.pack()

Now, we shall define login. Everything is pretty much similar

def loggin():
    email = str(en1.get())
    password = str(en2.get())
    name = str(en3.get())
    login = tk.Label(text="")
    with open("info.json", "r") as f:
        users = json.load(f)
    if not email in users:
        login.config(text="You don't have an account, sign up instead!")
    else:
        passs = users[email]["Password"]
        if password != passs:
            login.config(text="Wrong credentials. It doesn't match what I've recorded")
        else:
            login.config(text="Success! You've logged in. Please wait, as the software is still in the early stage of development.\nYou might have to sign up again later. I'll let you know soon.")
    login.pack()
loginn = tk.Button(text = "Log in", command = loggin)
loginn.pack()

At the end, this one line of code will determine whether everything is going to work. Make sure to put it in your code at the end.

window.mainloop()

And that is the end, please don't copy this, I worked for 5 hours to understand this. I'm a beginner just like everyone else, but please do not copy this. Use it as an example to understand. Even if you do, please give credit. But mostly, don't.

Overcurious answered 28/9, 2020 at 17:46 Comment(0)
E
0
#Creating a GUI for entering name
def xyz():
    global a
    print a.get() 
from Tkinter import *
root=Tk()  #It is just a holder
Label(root,text="ENter your name").grid(row=0,column=0) #Creating label
a=Entry(root)           #creating entry box
a.grid(row=7,column=8)
Button(root,text="OK",command=xyz).grid(row=1,column=1)
root.mainloop()           #important for closing th root=Tk()

This is the basic one.

Expressly answered 1/3, 2014 at 12:57 Comment(0)
K
0

tkinter is a GUI Library, this code creates simple no-text buttons:

 import tkinter as tk
 class Callback:
     def __init__(self, color):
         self.color = color
     def changeColor(self): 
         print('turn', self.color)
 c1 = Callback('blue')
 c2 = Callback('yellow')
 B1 = tk.Button(command=c1.changeColor) 
 B2 = tk.Button(command=c2.changeColor) 
 B1.pack()
 B2.pack()
Keelykeen answered 21/7, 2018 at 17:8 Comment(0)
C
-3

Here is my method, to create a window with a button called "Hello!" and when that is closed, a new window opens with "Cool!"

from tkinter import *
def hello(event):
    print("Single Click, Button-l") 
def Cool(event):                           
    print("That's cool!")

widget = Button(None, text='Hello!')
widget.pack()
widget.bind('<Button-1>', Hello)
widget.mainloop()

widget = Button(None, text='Cool!')
widget.pack()
widget.bind('<Double-1>', Cool)
Chippy answered 16/12, 2016 at 3:7 Comment(1)
why do you have code after calling mainloop? That code will not do what you think it does. You also bind a method Hello to an event, but your method is named hello. Also, best practices say you shouldn't use bind on a button; it has a command attribute for connecting the button to a function.Byre

© 2022 - 2024 — McMap. All rights reserved.