python tkinter尝试简化标签内容

时间:2018-12-05 06:47:23

标签: python-3.x tkinter

所以我试图简化标签的内容,但是我找不到任何方法

Im trying to make it look like this but with just one Label

from tkinter import *
from datetime import date
import lib

detect_date = date.today()
hari = detect_date.strftime("%A")

myWindow = Tk()
myWindow.title("Jadwal Kuliah")

def main():
    if (hari == "Monday"):
        Label(myWindow, text=lib.list_hari[0], font="none 14").pack()
        Label(myWindow, text=lib.list_mapel[0] + "|" + lib.list_waktu[0] + "|" + lib.list_kelas[1], font="none 14").pack()
        Label(myWindow, text=lib.list_dosen[0], font="none 14").pack()
        Label(myWindow, text="--------------------------------------------", font="none 14").pack()
        Label(myWindow, text=lib.list_mapel[1] + "|" + lib.list_waktu[1] + "|" + lib.list_kelas[0], font="none 14").pack()
        Label(myWindow, text=lib.list_dosen[1], font="none 14").pack()
        Label(myWindow, text="--------------------------------------------", font="none 14").pack()
        Label(myWindow, text=lib.list_mapel[2] + "|" + lib.list_waktu[2] + "|" + lib.list_kelas[9], font="none 14").pack()
        Label(myWindow, text=lib.list_dosen[2], font="none 14").pack()

Label(myWindow, text="JADWAL HARI INI", font="none 16", relief="sunken").pack()
main()

1 个答案:

答案 0 :(得分:0)

我想做的是将按钮,标签或实际上我想要的任何东西都半均匀地转换成函数,以通过预设设置将该对象返回给我。示例:

def my_label(frame, text):
    the_label = tkinter.Label(frame, text=text, font='none 14', fg='red')
    return the_label


first_label = my_label(myWindow, 'Some text here')
second_label = my_label(myWindow, 'Some other text here')

将返回带有这些字体和颜色设置等的标签。使它们保持标准。

但是,当前,从链接的图像看来,您一次使用多个标签来填充整个页面的条件。我要清理的是使用tkinter.Text()方法,这将使您能够制作一个可以填充的好大小的框。通过使用Text()方法,您可以...

from tkinter import *
from datetime import date
import lib

detect_date = date.today()
hari = detect_date.strftime("%A")

myWindow = Tk()
myWindow.title("Jadwal Kuliah")

def return_text_object(frame, text):
    text_object = Text(frame, font='none 14', fg='black')
    text_object.insert(text)
    return text_object

def main():
    if (hari == "Monday"):
        data_to_display = f'{text=lib.list_hari[0]}\n{lib.list_mapel[0]}|lib.list_waktu[0]}|{lib.list_kelas[1]}\n{lib.list_dosen[0]}\n--------------------------------------------\n{lib.list_mapel[1]}|{lib.list_waktu[1]}|{lib.list_kelas[0]}\n{lib.list_dosen[1]}\n--------------------------------------------\n{lib.list_mapel[2]}|{lib.list_waktu[2]}|{lib.list_kelas[9]}\n{lib.list_dosen[2]}'

        text_output = return_text_object(myWindow,data_to_display)
        text_output.pack()

Label(myWindow, text="JADWAL HARI INI", font="none 16", relief="sunken").pack()
main()