GUI:用frame使用联系,设置四个竖着排列的按钮,里面分别填上《静夜思》这首诗
- User
GUI:用frame使用联系,设置四个竖着排列的按钮,里面分别填上《静夜思》这首诗
- Assistant
import tkinter as tk
class Application(tk.Frame): def init(self, master=None): super().init(master) self.master = master self.pack() self.create_widgets()
def create_widgets(self): poem1 = "床前明月光," poem2 = "疑是地上霜。" poem3 = "举头望明月," poem4 = "低头思故乡。" self.btn1 = tk.Button(self, text=poem1, height=2, width=20) self.btn2 = tk.Button(self, text=poem2, height=2, width=20) self.btn3 = tk.Button(self, text=poem3, height=2, width=20) self.btn4 = tk.Button(self, text=poem4, height=2, width=20) self.btn1.pack(side="top") self.btn2.pack(side="top") self.btn3.pack(side="top") self.btn4.pack(side="top")root = tk.Tk() app = Application(master=root) app.mainloop()
- User
GUI:用frame使用联系,设置5个竖着排列的按钮,里面分别填上《望庐山瀑布》这首诗
- Assistant
import tkinter as tk
class Application(tk.Frame): def init(self, master=None): super().init(master) self.master = master self.pack() self.create_widgets()
def create_widgets(self): poem1 = "日照香炉生紫烟," poem2 = "遥看瀑布挂前川。" poem3 = "飞流直下三千尺," poem4 = "疑是银河落九天。" poem5 = "挂席卧听岩溪冷," self.btn1 = tk.Button(self, text=poem1, height=2, width=30) self.btn2 = tk.Button(self, text=poem2, height=2, width=30) self.btn3 = tk.Button(self, text=poem3, height=2, width=30) self.btn4 = tk.Button(self, text=poem4, height=2, width=30) self.btn5 = tk.Button(self, text=poem5, height=2, width=30) self.btn1.pack(side="top") self.btn2.pack(side="top") self.btn3.pack(side="top") self.btn4.pack(side="top") self.btn5.pack(side="top")root = tk.Tk() app = Application(master=root) app.mainloop()
- User
GUI:编写一个程序,创建一个窗口,窗口顶部用红色字体标示“我的照片”,并在下方配上一张图
- Assistant
以下是一个实现窗口顶部用红色字体标示“我的照片”,并在下方配上一张图片的程序:
import tkinter as tk from PIL import Image, ImageTk # 创建窗口 root = tk.Tk() # 设置窗口标题 root.title("My photo") # 设置窗口大小 root.geometry("600x600") # 窗口顶部标示 label = tk.Label(root, text="我的照片", font=("Arial", 30), fg="red") label.pack(pady=20) # 加载图片 image = Image.open("photo.jpg") photo = ImageTk.PhotoImage(image) # 创建标签,并插入图片 label_photo = tk.Label(root, image=photo) label_photo.pack(pady=20) # 进入消息循环 root.mainloop()注意,上述程序需要先安装 Pillow 库,可以使用以下命令安装:
pip install Pillow其中,
photo.jpg是图片文件的文件名,需要放在程序所在目录下。 - User
GUI:编写一个程序,创建一个窗口,并配上一张图(铺满窗口),在图片上用红色字体标示“我是杨慧敏”
- Assistant
以下是一个实现在图片上用红色字体标示“我是杨慧敏”的程序:
import tkinter as tk from PIL import Image, ImageTk # 创建窗口 root = tk.Tk() # 设置窗口标题 root.title("My Photo") # 创建画布 canvas = tk.Canvas(root, width=600, height=600) canvas.pack() # 加载图片 image = Image.open("photo.jpg") photo = ImageTk.PhotoImage(image) # 在画布上添加图片 canvas.create_image(0, 0, anchor='nw', image=photo) # 在图片上添加文本 canvas.create_text(300, 300, text="我是杨慧敏", font=("Arial", 30), fill="red") # 进入消息循环 root.mainloop()注意,上述程序需要先安装 Pillow 库,可以使用以下命令安装:
pip install Pillow其中,
photo.jpg是图片文件的文件名,需要放在程序所在目录下。