#368 Python TKinter, QT6 and PySimpleGUI Code Examples

#368 Python TKinter, QT6 and PySimpleGUI Code Examples

#368 Provide sample python code using tkinter to create a gui with a title, text input, submit button, quit button, text box and yes/no radio control

Here is sample code that creates a GUI using the tkinter library in Python, with a title, text input, submit button, quit button, text box, and yes/no radio control:

import tkinter as tk


def on_submit():

    text = input_text.get()

    text_box.insert(tk.END, text + '\n')

    input_text.delete(0, 'end')


root = tk.Tk()

root.title("My GUI")


label = tk.Label(root, text="Enter text:")

label.pack()


input_text = tk.Entry(root)

input_text.pack()


submit_button = tk.Button(root, text="Submit", command=on_submit)

submit_button.pack()


quit_button = tk.Button(root, text="Quit", command=root.quit)

quit_button.pack()


text_box = tk.Text(root)

text_box.pack()


var = tk.StringVar()

var.set("Yes")


yes_radio = tk.Radiobutton(root, text="Yes", variable=var, value="Yes")

yes_radio.pack()


no_radio = tk.Radiobutton(root, text="No", variable=var, value="No")

no_radio.pack()


root.mainloop()

This code creates a window with a title "My GUI", a label "Enter text:", an input box, a submit button, and a quit button. When the submit button is pressed, the text from the input box is inserted into the text box and the input box is cleared. The yes/no radio control is created with a default value of "Yes", and the value can be changed by selecting the other radio button.


Repeat for QT6

Here is sample code that creates a GUI using the PyQt6 library in Python, with a title, text input, submit button, quit button, text box, and yes/no radio control:

import sys

from PyQt6.QtCore import Qt

from PyQt6.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QLineEdit, QTextEdit, QRadioButton, QPushButton


class MyGui(QDialog):

    def __init__(self):

        super().__init__()


        self.input_text = QLineEdit(self)

        self.submit_button = QPushButton("Submit", self)

        self.submit_button.clicked.connect(self.on_submit)

        self.quit_button = QPushButton("Quit", self)

        self.quit_button.clicked.connect(self.close)

        self.text_box = QTextEdit(self)

        self.yes_radio = QRadioButton("Yes", self)

        self.yes_radio.setChecked(True)

        self.no_radio = QRadioButton("No", self)


        layout = QVBoxLayout(self)

        layout.addWidget(QLabel("Enter text:"))

        layout.addWidget(self.input_text)

        layout.addWidget(self.submit_button)

        layout.addWidget(self.quit_button)

        layout.addWidget(self.text_box)

        layout.addWidget(self.yes_radio)

        layout.addWidget(self.no_radio)


    def on_submit(self):

        text = self.input_text.text()

        self.text_box.append(text)

        self.input_text.clear()


app = QApplication(sys.argv)

gui = MyGui()

gui.setWindowTitle("My GUI")

gui.show()

sys.exit(app.exec_())

This code creates a window with a title "My GUI", a label "Enter text:", an input box, a submit button, and a quit button. When the submit button is pressed, the text from the input box is inserted into the text box and the input box is cleared. The yes/no radio control is created with a default value of "Yes", and the value can be changed by selecting the other radio button. This code uses the PyQt6 package to build the GUI.


Repeat for tkinter adding a file select capability

Here is sample code that creates a GUI using the tkinter library in Python, with a title, text input, submit button, quit button, text box, yes/no radio control and file select capability:

import tkinter as tk

from tkinter import filedialog


def on_submit():

    text = input_text.get()

    text_box.insert(tk.END, text + '\n')

    input_text.delete(0, 'end')

    

def on_select_file():

    file_path = filedialog.askopenfilename()

    file_path_label.config(text = file_path)


root = tk.Tk()

root.title("My GUI")


label = tk.Label(root, text="Enter text:")

label.pack()


input_text = tk.Entry(root)

input_text.pack()


submit_button = tk.Button(root, text="Submit", command=on_submit)

submit_button.pack()


file_select_button = tk.Button(root, text="Select File", command=on_select_file)

file_select_button.pack()


file_path_label = tk.Label(root, text="No file selected")

file_path_label.pack()


quit_button = tk.Button(root, text="Quit", command=root.quit)

quit_button.pack()


text_box = tk.Text(root)

text_box.pack()


var = tk.StringVar()

var.set("Yes")


yes_radio = tk.Radiobutton(root, text="Yes", variable=var, value="Yes")

yes_radio.pack()


no_radio = tk.Radiobutton(root, text="No", variable=var, value="No")

no_radio.pack()


root.mainloop()

This code creates a window with a title "My GUI", a label "Enter text:", an input box, a submit button, a "select file" button and a quit button. When the submit button is pressed, the text from the input box is inserted into the text box and the input box is cleared. The yes/no radio control is created with a default value of "Yes", and the value can be changed by selecting the other radio button. When "Select File" button is pressed, a file dialog will open to select a file and the file path will be displayed in the "file_path_label"


Repeat the code example using PySimpleGUI

Here is sample code that creates a GUI using the PySimpleGUI library in Python, with a title, text input, submit button, quit button, text box, yes/no radio control and file select capability:

import PySimpleGUI as sg


def on_submit():

    text = input_text.Get()

    text_box.update(text + '\n')

    input_text.update("")


def on_select_file():

    file_path = sg.popup_get_file("Select a file", title = "File Selector", no_window = True)

    file_path_label.update(file_path)


layout = [[sg.Text("Enter text:"), sg.InputText(key = "input_text")],

          [sg.FileBrowse("Select File", key = "file_selector"), sg.Text("", key = "file_path")],

          [sg.Radio("Yes", "RADIO1", key = "yes", default = True), sg.Radio("No", "RADIO1", key = "no")],

          [sg.Multiline("", key = "text_box", size=(40, 10))],

          [sg.Button("Submit"), sg.Button("Quit")]

         ]


window = sg.Window("My GUI", layout)


while True:

    event, values = window.read()

    if event in (None, 'Quit'):

        break

    if event == "Submit":

        on_submit()

    if event == "file_selector":

        on_select_file()


window.close()

This code creates a window with a title "My GUI", a label "Enter text:", an input box, a submit button, a "Select File" button and a quit button. When the submit button is pressed, the text from the input box is inserted into the text box and the input box is cleared. The yes/no radio control is created with a default value of "Yes", and the value can be changed by selecting the other radio button. When "Select File" button is pressed, a file dialog will open to select a file and the file path will be displayed in the "file_path_label" .

PySimpleGUI is a wrapper for tkinter, PyQt, wxPython and Remi that makes it easy to create a graphical user interface (GUI) for your Python programs. It provides a simple way to create common GUI elements like buttons, inputs, and labels, and it can be customized to create more complex layouts.

It's important to note that this is a basic example, and you will need to add error handling and validation to make it robust in a production environment.


From <https://chat.openai.com/chat>

Comments

Popular posts from this blog

#2024 More: IDEAfier.com

#1453 Potential Fun Topics to Pursue in Chat

#2568 Job Description vs Resume