tkinterでモーダルダイアログを作成するにはどうすればよいですか?
ダイアログボックスは、あらゆるアプリケーションの非常に重要なコンポーネントです。これは通常、ユーザーおよびアプリケーションインターフェイスと対話するために使用されます。トップレベルウィンドウやその他のウィジェットを使用して、任意のtkinterアプリケーションのダイアログボックスを作成できます。トップレベルウィンドウは、他のすべてのウィンドウの上にあるものをポップアップします。したがって、ダイアログボックスを作成するためのトップレベルウィンドウにさらに多くのものを追加できます。
例
この例では、2つの部分からなるモーダルダイアログを作成しました。
- トップレベルウィンドウの初期化。
- ポップアップダイアログイベントの関数定義。
- トップレベルウィンドウにウィジェットを追加します。
- ダイアログオプションの関数定義。
# Import required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the window size
win.geometry("700x350")
style = ttk.Style()
style.theme_use('clam')
# Define a function to implement choice function
def choice(option):
pop.destroy()
if option == "yes":
label.config(text="Hello, How are You?")
else:
label.config(text="You have selected No")
win.destroy()
def click_fun():
global pop
pop = Toplevel(win)
pop.title("Confirmation")
pop.geometry("300x150")
pop.config(bg="white")
# Create a Label Text
label = Label(pop, text="Would You like to Proceed?",
font=('Aerial', 12))
label.pack(pady=20)
# Add a Frame
frame = Frame(pop, bg="gray71")
frame.pack(pady=10)
# Add Button for making selection
button1 = Button(frame, text="Yes", command=lambda: choice("yes"), bg="blue", fg="white")
button1.grid(row=0, column=1)
button2 = Button(frame, text="No", command=lambda: choice("no"), bg="blue", fg="white")
button2.grid(row=0, column=2)
# Create a Label widget
label = Label(win, text="", font=('Aerial', 14))
label.pack(pady=40)
# Create a Tkinter button
ttk.Button(win, text="Click Here", command=click_fun).pack()
win.mainloop() 出力
上記のコードを実行すると、モーダルダイアログボックスを開くためのボタンが付いたウィンドウが表示されます。
ボタンをクリックすると、[モーダル]ダイアログボックスが開きます。
-
Tkinterウィジェットを非表示にするにはどうすればよいですか?
tkinterウィジェットを非表示にするには、 pack_forget()を使用できます。 方法。通常、ウィンドウからウィジェットのマップを解除するために使用されます。 例 次の例では、ラベルテキストと、ラベルテキストウィジェットで非表示のイベントをトリガーするために使用できるボタンを作成します。 #Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Set the geometry of frame win.geometry("6
-
Tkinterを使用して簡単な画面を作成するにはどうすればよいですか?
Tkinterライブラリを使用して簡単な画面を作成します。 アルゴリズム Step 1: Import tkinter. Step 2: Create an object of the tkinter class. Step 3: Display the screen. サンプルコード import tkinter as tk window = tk.Tk() 出力