如何使用PythonTkinter打开空白窗口?

2024-03-28 09:00:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,所以我没有任何尝试或测试过的代码。你们知道如何打开一个可以用Python运行的空白窗口吗?我知道可能有人问过这个问题,但我找不到。多谢各位


Tags: 代码空白新手
2条回答

使用tkinter可以打开一个空白窗口:

from tkinter import *

root = Tk()
root.title("Hello this is title")
root.geometry("500x300")

root.mainloop()

试试这个,应该很好:

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):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

相关问题 更多 >