How to make a button window in python

How to create a function that creates a window with two buttons, where each button has a specified line and, if pressed, returns the specified variable? Like @ 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 this is a tutorial for a very simple program for beginners, but this is the only video i), but without a text box, and I have more controls that the "ok" and "cancel" buttons do.

I need to create a window, draw a rectangle with a line inside it, and then create a loop that checks mouse movement / mouse clicks, and then return something when the mouse coordinates are inside one of the buttons, and click? Or is there a function / set of functions that would facilitate a button window? Or a module?

+4
source share
4 answers

Overview

, " , ". , , , . , , , . . , , .

, , , . . , , . , , , , .

Tkinter

, - tkinter. Tkinter - , python. , wxPython, PyQT .., . Tkinter , , , , . , , . .

Tkinter. python 2.x. python 3.x tkinter, 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()
+8

wxpython, , , python.

():

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()

( ). .

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()

.

0

Here is my method to create a window with the "Hello!" Button and when it is closed, a new window will open with the inscription "Cool!"

from tkinter import *
def hello(event):
    print("Single Click, Button-l") 
def Cool(event):                           
    print("That 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)
-2
source

Source: https://habr.com/ru/post/1529507/


All Articles