【问题标题】:How can I make my python window more presentable?如何让我的 python 窗口更美观?
【发布时间】:2020-09-19 01:45:01
【问题描述】:

我在高中学习 python,所以如果我的问题的答案似乎很明显或者我的代码草率,我很抱歉。

我在 python 中使用 tkinter 创建一个窗口,用户可以在其中放置他拥有的成分,当单击确认时,该窗口将在画布内显示可供他们使用的食谱。这是它目前的样子(抱歉,语言是法语,但应该是可读的)

我的代码如下所示:

def takequantity():
 mondico["Tomate"] = saisirtomate.get()
 mondico["Fromage"] = saisirfromage.get()
 mondico["Farine"]= saisirfarine.get()
 #I copy and paste this as many times as I need for about 20 ingrediants

 if (mondico["Tomate"]>=5 and
     mondico["Farine"]>=500 and
     mondico["Fromage"]>=50):
    recette=can.create_text(300, 200, text="Pizza")
    #basically, if the ingrediants pass a certain threshold, it will recommend the Pizza recipe


mondico = {}
mondico["Tomate"] = 0
mondico["Farine"] = 0
mondico["Fromage"] = 0
#I copy and paste this as many times as I need for about 20 ingrediants

在这个下面,我有这个:

import tkinter as tk

window=tk.Tk()
window.geometry("900x900")
window.minsize(height=900 ,  width=900)
window.config(background='#B8FAEF')


frame=tk.Frame(window, bg='#B8FAEF')

label_title = tk.Label(window, text="SmartFridgoo", font=("Comic Sans MS", 20))
label_title.pack()


label_subtitle = tk.Label(window, text="Veuillez entrez vos ingrédients", font=("Comic Sans MS", 10))
label_subtitle.pack()

frame.pack(expand=1)


Texttomate=tk.Label(window,text="Tomates",)
Texttomate.pack()
saisirtomate=tk.IntVar()
saisietomate=tk.Entry(textvariable=saisirtomate, width=9, justify='center')
saisietomate.pack()
#I copy and paste this, you get the idea

bouton=tk.Button(window,text="Confirmer et faire apparaître les recettes", command=takequantity)
bouton.pack()



can=tk.Canvas(window, bg='#B8FAEF', height=400, width=600)
can.pack()


window.mainloop()

我想要的是,您放置数字的插槽不会相互堆叠并像那样对齐。网格形成会很好,但我不知道该怎么做。 我也想让食谱出现在画布的中心,但如果有多个食谱可能,我希望它们排列整齐,而不是彼此重叠。我该怎么做?

如果我做错了什么,我深表歉意,这是我在这里的第一篇文章。

编辑:我还希望能够将链接附加到显示的文本中。例如,当您单击“披萨”时,它会将您重定向到 youtube 食谱。我查找了如何使用 .bind 进行操作,但它不适用于“can.create_text”中的文本

【问题讨论】:

  • 元素的定位,this site帮了我。
  • 不要使用comic sans。永远。

标签: python user-interface tkinter


【解决方案1】:

在 tkinter 检查effbot.org 中,网格的形成非常容易@ 很酷的是,您可以在主窗口和框架内使用网格。例如,您可以使用widg.grid() 创建几个框架并在其中插入小部件,然后也使用网格将框架放置在窗口中。

【讨论】:

    猜你喜欢
    • 2019-01-18
    • 1970-01-01
    • 2021-06-06
    • 2020-02-22
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多