My first thought was what are the GUI options for Python, so I asked:
How to generate a gui interface from python code?
What resulted from this request was:
There are several libraries that can be used to generate a GUI interface from Python code, some of the most popular ones include:
Tkinter: This is a built-in Python library that provides a simple way to create graphical user interfaces. It is a powerful tool for creating desktop applications.
PyQt: This is a set of Python bindings for the Qt application framework and runs on all platforms supported by Qt including Windows, OS X, Linux, iOS, and Android.
wxPython: This is a set of Python bindings for the wxWidgets toolkit, which allows you to create graphical user interfaces that are compatible with Windows, OS X, and Linux.
PyGTK: PyGTK is a set of Python bindings for the GTK toolkit. It is the foundation for the GNOME desktop environment and is compatible with Windows, OS X, and Linux.
PySide: PySide is a set of Python bindings for the Qt toolkit and runs on all platforms supported by Qt including Windows, OS X, Linux, iOS, and Android.
Each of these libraries has its own strengths and weaknesses, so you should choose the one that best suits your needs.
That was a pretty useful overview of the options, so I thought I’d just let it take a crack at it and asked:
write a graphic user interface in python that rolls two dice
What I got back definitely worked:
import tkinter as tk
import random
def roll_dice():
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
result_label.config(text=f"Dice 1: {dice1}\nDice 2: {dice2}")
root = tk.Tk()
root.title("Dice Roller")
result_label = tk.Label(root, text="Welcome to Dice Roller", font=("Helvetica", 20))
result_label.pack()
roll_button = tk.Button(root, text="Roll Dice", command=roll_dice)
roll_button.pack()
root.mainloop()
This GUI has a button labeled "Roll Dice" and a label that displays the results of the dice roll (e.g. "Dice 1: 4\nDice 2: 5"). When the button is clicked, the roll_dice() function is called, which generates random numbers between 1 and 6 for each dice and updates the label with the results.
This is just a simple example, you can customize it as you like and add more functionality, for example you can add a history of rolls or a picture of the dices, etc.
No comments, but when I ran it, the GUI generated showed:

And when I clicked Roll Dice it displayed a window:

And new random numbers were displayed every time I clicked Roll Dice. Not too bad with the level of direction I gave it. So, I asked it to improve upon the design, by stating:
add to the GUI the ability to have pictures of the dice
It already knew about the previous program so I was hoping it would just build upon that result. This was what was generated.
import tkinter as tk
from PIL import Image, ImageTk
import random
def roll_dice():
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
dice1_image = ImageTk.PhotoImage(Image.open(f"dice{dice1}.png"))
dice2_image = ImageTk.PhotoImage(Image.open(f"dice{dice2}.png"))
dice1_label.config(image=dice1_image)
dice2_label.config(image=dice2_image)
dice1_label.image = dice1_image
dice2_label.image = dice2_image
root = tk.Tk()
root.title("Dice Roller")
result_label = tk.Label(root, text="Welcome to Dice Roller", font=("Helvetica", 20))
result_label.pack()
dice1_label = tk.Label(root)
dice1_label.pack(side=tk.LEFT)
dice2_label = tk.Label(root)
dice2_label.pack(side=tk.RIGHT)
roll_button = tk.Button(root, text="Roll Dice", command=roll_dice)
roll_button.pack()
root.mainloop()
Here, I'm using the PIL library to open and display images of dice. I'm assuming that the images are in the same folder as the script, and are named dice1.png, dice2.png, etc.
In the roll_dice() function, I'm generating random numbers between 1 and 6 for each dice, and then creating PhotoImage objects for each dice using the ImageTk.PhotoImage method. I'm then configuring the dice1_label and dice2_label to display the corresponding images and updating the labels' images.
You can use your own images of the dices, just make sure that the images are in the same folder as the script and the name of the images match the code, for example if you have a dice with the number 3, the image should be named dice3.png
Keep in mind that the images must be in a format that the PIL library can open, like PNG, JPG, etc.
I now had to find some dice images for it to display, which I just pulled off the internet into the folder with the file names stated. I dropped those into the folder, so now the display looked like:

This is better, so now I realize it is not compatible with Android, one platform where I would like this to run. I asked it:
write a graphic user interface in python that rolls two dice PyQt
and the following was generated:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget
from random import randint
class DiceRoller(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Dice Roller")
self.initUI()
def initUI(self):
self.dice1_label = QLabel()
self.dice1_label.setAlignment(Qt.AlignCenter)
self.dice2_label = QLabel()
self.dice2_label.setAlignment(Qt.AlignCenter)
roll_button = QPushButton("Roll Dice")
roll_button.clicked.connect(self.roll_dice)
layout = QVBoxLayout()
layout.addWidget(self.dice1_label)
layout.addWidget(self.dice2_label)
layout.addWidget(roll_button)
self.setLayout(layout)
def roll_dice(self):
dice1 = randint(1, 6)
dice2 = randint(1, 6)
self.dice1_label.setPixmap(QPixmap(f"dice{dice1}.png"))
self.dice2_label.setPixmap(QPixmap(f"dice{dice2}.png"))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = DiceRoller()
window.show()
sys.exit(app.exec_())
This GUI has a button labeled "Roll Dice" and two labels that displays the results of the dice roll (dice images). When the button is clicked, the roll_dice() function is called, which generates random numbers between 1 and 6 for each dice, loads the images using the QPixmap class and updates the labels with the images.
As in the previous example, I'm assuming that the images are in the same folder as the script, and are named dice1.png, dice2.png, etc. and you can use your own images, just make sure the name of the images match the code, for example if you have a dice with the number 3, the image should be named dice3.png.
Keep in mind that the images must be in a format that PyQt can open, like PNG, JPG, etc.
If this code is error free (and it should be), that definitely took some of the grunt work out of learning GUI programming with Python. I’ll have to get back to this tomorrow though.
[…] The Meta AI chief scientist may have access to “what’s available at other labs.”, but I don’t, so I am continuing my path to discovery with what I have. Taking up where I left off in the previous post about ChatGPT’s ability to aid software development: […]
LikeLike
[…] with ChatGPT, and Bing Chat came out, I thought I’d try it. I already had a baseline dealing with writing Python for dice rolling, so I thought I’d start with the same kind of query for Bing […]
LikeLike