Tkinterでポップアップメニューを作成するにはどうすればよいですか?
ユーザーの操作が必要なアプリケーションにはメニューバーが必要です。メニューは、メニュー(親)を初期化することで作成できます メニュー項目と一緒にオブジェクト。ポップアップメニューは、 tk_popup(x_root、y_root、False)を初期化することで作成できます。 これにより、メニューが画面に表示されます。次に、マウスボタン(右クリック)でトリガーできるイベントを追加します。 grab_release() メソッドは、マウスボタンのリリースを設定してポップアップメニューの設定を解除します。
例
#Import the required libraries
from tkinter import *
from tkinter import ttk
#Create an instance of Tkinter frame
win = Tk()
#Set the geometry of the Tkinter library
win.geometry("700x350")
label = Label(win, text="Right-click anywhere to display a menu", font= ('Helvetica 18'))
label.pack(pady= 40)
#Add Menu
popup = Menu(win, tearoff=0)
#Adding Menu Items
popup.add_command(label="New")
popup.add_command(label="Edit")
popup.add_separator()
popup.add_command(label="Save")
def menu_popup(event):
# display the popup menu
try:
popup.tk_popup(event.x_root, event.y_root, 0)
finally:
#Release the grab
popup.grab_release()
win.bind("<Button-3>", menu_popup)
button = ttk.Button(win, text="Quit", command=win.destroy)
button.pack()
mainloop() 出力
上記のコードを実行すると、ラベルとボタンのあるウィンドウが表示されます。マウスで右クリックすると、ウィンドウにポップアップメニューが表示されます。
-
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() 出力
-
Tkinterを使用してスプラッシュ画面を作成するにはどうすればよいですか?
tkinterを使用してスプラッシュ画面を作成するとします。スプラッシュ画面を作成するには、以下の手順に従います- いくつかのラベルを含むスプラッシュ画面を作成します。 overrideredirect を使用して、スプラッシュ画面をフチなしにします メソッド。 スプラッシュ画面の直後に一時的に表示されるメインウィンドウの関数を作成します。 現在、後を使用しています メソッドを使用すると、メインウィンドウが表示される時間を定義できます。 例 #Importing the tkinter library from tkinter import * #Create