PythonでGUIを作成する際には、Tkinterがよく使用されます。しかし、Tkinterのデザインは少々古めかしいと感じる方もいるかもしれません。そこで、今回はTomSchimanskyのCustomTkinterというライブラリを使用して、おしゃれで使いやすいGUIを作成してみましょう。
まずは、CustomTkinterをインストールします。
$ pip install customtkinter
次に、以下のサンプルコードを参考に、自分だけのGUIを作成してみてください。
import customtkinter
FONT_TYPE = "meiryo"
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.fonts = (FONT_TYPE, 15)
self.geometry("350x200")
self.title("Basic GUI")
self.setup_form()
def setup_form(self):
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("blue")
self.textbox = customtkinter.CTkEntry(master=self, placeholder_text="テキストを入力してください", width=220, font=self.fonts)
self.textbox.place(x=60, y=50)
self.button = customtkinter.CTkButton(master=self, text="クリックしてね", command=self.button_function, font=self.fonts)
self.button.place(x=100, y=100)
def button_function(self):
print(self.textbox.get())
if __name__ == "__main__":
app = App()
app.mainloop()
このサンプルコードでは、ウィジェットの配置にplace
を使用していますが、place
は極力使用せず、grid
やpack
を使用することをおすすめします。
また、PySimpleGUIもGUI作成に便利なライブラリです。PySimpleGUIでは、sg.Column
とsg.Frame
を使用して、複雑なレイアウトを作成することができます。
以上がPythonでGUIレイアウトを作成する方法の一例です。Pythonの強力なライブラリを活用して、自分だけのGUIを作成してみてください。.