0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

備忘録(Python)

Last updated at Posted at 2024-04-21

辞書型を1つの文字列として返す関数

screen_dict.py
def screen_menu(dict):
  txt = ""
  for key,value in dict.items():
    txt +=value+" : "+str(key)+"\n"
  return txt

これをtkinterのtextに利用!

tkinterのlabelのテキストに辞書型を入れたかったから作った。

import tkinter as tk
import tkinter.messagebox as tmsg

def screen_menu(menu):
  txt = ""
  for key,value in menu.items():
    txt +=key+" : "+str(value)+"\n"
  return txt

main = {"ラーメン":600,"餃子":400,"チャーハン":500}
root = tk.Tk()
root.geometry = ("800x400+500+300")
root.title("ラーメン屋")
screen_b = tk.Frame(root,width=800,height=600)


label_b = tk.Label(screen_b,text = "メニュー")
label_b.pack()

label_b_2 = tk.Label(screen_b,text=screen_menu(main),font=("Times",12))
label_b_2.pack()
screen_b.pack()

root.mainloop()

 

実行すると

スクリーンショット 2024-04-21 195325.png

こんな感じ^^

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?