Write a program to display font sizes Layout It should have: ● In the top: - label for letter “X” - label for font size value ● In the bottom: - button to increase font size - button to decrease font size Desired final look: Functionality Initially, the X label should display an 18 point “X”, and the font size label should display the value “18”. Every time the “increase” button is selected, the font size of the “X” and the value in the font size label should be increased by 1. Every time the “decrease” button is selected, the font size of the “X” and the value in the font size label should be decreased by 1. Make sure that this does not allow negative font sizes.
时间: 2024-01-15 11:03:15 浏览: 51
Sure, I can help you with that! Here's a sample code in Python using the Tkinter library:
```
from tkinter import *
class FontSizer:
def __init__(self, master):
self.master = master
master.title("Font Sizer")
# create top label for "X"
self.x_label = Label(master, text="X", font=("Helvetica", 18))
self.x_label.pack(pady=10)
# create label for font size value
self.font_size_label = Label(master, text="18", font=("Helvetica", 14))
self.font_size_label.pack(pady=10)
# create bottom button to increase font size
self.increase_button = Button(master, text="Increase Font Size", command=self.increase_font_size)
self.increase_button.pack(side=LEFT, padx=10, pady=10)
# create bottom button to decrease font size
self.decrease_button = Button(master, text="Decrease Font Size", command=self.decrease_font_size)
self.decrease_button.pack(side=RIGHT, padx=10, pady=10)
def increase_font_size(self):
font_size = int(self.font_size_label["text"])
font_size += 1
self.update_font_size(font_size)
def decrease_font_size(self):
font_size = int(self.font_size_label["text"])
if font_size > 1:
font_size -= 1
self.update_font_size(font_size)
def update_font_size(self, font_size):
self.x_label.config(font=("Helvetica", font_size))
self.font_size_label.config(text=str(font_size))
# create the main window
root = Tk()
# create an instance of the FontSizer class
font_sizer = FontSizer(root)
# start the GUI
root.mainloop()
```
This program creates a window with a label for the letter "X" and a label for the font size value. It also has two buttons at the bottom to increase or decrease the font size. The font size of the "X" label and the font size value label are updated whenever the user clicks on the increase or decrease buttons.
To use this program, save the code in a file with a ".py" extension and run it using a Python interpreter.
阅读全文