8月27日(My Menuの骨格, 改行)

ページ名:8月27日(卒業制作)

Label内の改行

2種類の方法があります

  • Labelのwraplengthプロパティを用いる
  • テキスト内に改行記号\nを書いておく

 

import tkinter as tk

mondai = '朝来市佐嚢の出身で第百銀行の頭取を務めた人物は誰でしょう。朝来市佐嚢の出身で第百銀行の頭取を務めた人物は誰でしょう。'
mondai2 = '朝来市佐嚢の出身で\n第百銀行の頭取を務めた人物は誰でしょう。\n朝来市佐嚢の出身で\n第百銀行の頭取を務めた人物は誰でしょう。'

root = tk.Tk()
root.geometry("400x300") #ウィンドウサイズ
tk.Label(root,text = mondai, wraplength = 200).pack() #長さ200で改行する
tk.Label(root,text = mondai2).pack() #任意の場所(\n)で改行する
root.mainloop()

My Menuの骨格

#レシピ提案プログラム My Menu
#2019-08-27
import os
import tkinter as tk

zairyo = {
    '野菜':['大根','人参','ジャガイモ','タマネギ'],
    '肉':['牛肉','豚肉','鶏肉','合挽肉']
}

menus = {
    '酢鶏':['鶏肉', '卵', '大根'],
    '筑前煮':['人参', '鶏肉', '蓮根', '大根', '絹さや'],
    'チキンステーキ':['鶏肉','大根','大葉'],
    'ハンバーグ':['合挽肉','タマネギ']
}

root = tk.Tk()
root.geometry("400x300") #ウィンドウサイズ

selectedZairyo = [] #選んだ材料

def getZairyoLabelText():
    global selectedZairyo
    text = '使う材料:'
    if not selectedZairyo: #selectedZairyoが空だったら
        text += 'なし'
    else:
        text += ','.join(selectedZairyo)
    text += '。他に使う材料は?'
    return text
    
def showZairyo(category):
    def showZairyoF(): #cf: http://memopy.hatenadiary.jp/entry/2017/06/11/220452
        global itemsButtons
        for item in zairyo[category]:
            itemButton = tk.Button(categoryFrames[category], text = item, command = selectZairyo(category,item))
            itemButton.pack()
            itemsButtons[category][item] = itemButton
    return showZairyoF

def selectZairyo(category,item):
    def selectZairyoF():
        global selectedZairyo
        selectedZairyo.append(item)
        for itm in zairyo[category]:
            itemsButtons[category][itm].pack_forget() #表示しているアイテムボタンを消す
        zairyoLabel['text'] = getZairyoLabelText()
    return selectZairyoF

def showMenu():
    global selectedZairyo
    hideZairyoButtons()
    zairyoLabel['text'] = ','.join(selectedZairyo) + 'を使ったメニュー:'
    for menu, zairyo in menus.items():
        if set(selectedZairyo).issubset(set(zairyo)):
            menuFrame = tk.Frame(root)
            menuFrame.pack()
            menuFrames[menu] = menuFrame
            menuButton = tk.Button(menuFrame, text = menu, command = showRecipe(menu))
            menuButton.pack(side="left")

def hideZairyoButtons():
    for category in zairyo:
        button = categoryButtons[category]
        button.pack_forget()

def showRecipe(menu):
    def showRecipeF():
        zairyoLabel['text'] = menu + 'の材料は' + ','.join(menus[menu]) + 'です。'
    return showRecipeF
    
zairyoLabel = tk.Label(root)
zairyoLabel['text'] = getZairyoLabelText()
zairyoLabel.pack()
zairyoFrame = tk.Frame(root)
zairyoFrame.pack()
categoryFrames = {}
menuFrames = {}
categoryButtons = {}
itemsFrames = {}
itemsButtons = {}
for category in zairyo:
    categoryFrame = tk.Frame(zairyoFrame)
    categoryFrame.pack(side="left")
    categoryFrames[category] = categoryFrame
    categoryButton = tk.Button(categoryFrame, text = category, command = showZairyo(category))
    categoryButton.pack(side="top")
    categoryButtons[category] = categoryButton
    itemsFrame = tk.Frame(categoryFrame)
    itemsFrame.pack()
    itemsFrames[category] = itemsFrame
    itemsButtons[category] = {}
categoryFrame = tk.Frame(zairyoFrame)
categoryFrame.pack(side="left")
zairyoOkButton = tk.Button(categoryFrame, text = '以上', command = showMenu)
zairyoOkButton.pack(side="left")
root.mainloop()

シェアボタン: このページをSNSに投稿するのに便利です。

コメント

返信元返信をやめる

※ 悪質なユーザーの書き込みは制限します。

最新を表示する

NG表示方式

NGID一覧