2017-05-06 98 views
0

我在寫我的第一個編程代碼。我想知道是否可以在框架內添加文本和圖像標籤。我創建了一個畫布並添加了兩個框架,然後嘗試添加圖像和文本文件(要顯示在畫布的頂部),但文字和圖片不顯示。當我運行沒有框架的程序時,它確實顯示。下面是代碼:如何在Python中使用tkinter在框架中添加文本和圖像

from tkinter import * 
from tkinter import ttk 

root = Tk() 
root.title ('iMedic') 
canvas = Canvas(root, width = 1600, height = 800) 


panewindow = ttk.Panedwindow(canvas, orient = VERTICAL) 
panewindow.pack(fill = BOTH, expand = True) 
paitents_frame = ttk.Frame(panewindow, width = 1600, height = 400, relief = RAISED) 
prescription_frame = ttk.Frame(panewindow, width = 1600, height = 300, relief = RAISED) 
panewindow.add(paitents_frame, weight = 1) 
panewindow.add(prescription_frame, weight = 1) 

canvas.grid(row = 0, column = 0) 
photo = PhotoImage(file = './logo.gif') 
canvas.create_image(55, 55, image=photo) 
canvas.create_text(600, 155, text = 'Welcome', font = ('Helvetica', 72, 'bold'), justify = 'center', fill='blue') 
canvas.update 

root.mainloop() 

有沒有辦法可以解決這個問題?我會假設另一種方法是將圖片和文字置於頂部,然後在其下方添加框架,但我不知道如何去做。謝謝!

回答

0

我不清楚爲什麼你要將畫框添加到畫布上,但是要用後面的語句;

我會假設另一種方法是將圖片和文字放在上面,然後在下面添加相框,但我不知道該怎麼做。

這裏是你如何能做到這一點:

  1. 使root代替帆布孩子的panewindow孩子
  2. 的幀大小以適合他們的內容,所以我加了兩個標籤中的每個 到讓它們可見,那麼您應該將這些標籤替換爲您需要的任何一個小組件。
  3. 我對所有窗口小部件展示位置使用了pack,但您可以用grid替換它們,並提供相應的rowcolumn值。

**

from tkinter import * 
from tkinter import ttk 

root = Tk() 
root.title ('iMedic') 
canvas = Canvas(root, width = 1600, height = 250) 


canvas.pack(fill = BOTH, expand = True) 
photo = PhotoImage(file = './logo.gif') 
canvas.create_image(55, 55, image=photo) 
canvas.create_text(600, 155, text = 'Welcome', font = ('Helvetica', 72, 'bold'), justify = 'center', fill='blue') 
canvas.update 

# Make panewindow child of root 
panewindow = ttk.Panedwindow(root, orient = VERTICAL) 
panewindow.pack(fill = BOTH, expand = True) 

# paitents_frame with Labels in it 
paitents_frame = ttk.Frame(panewindow, width = 1600, height = 400, relief = RAISED) 
paitents_label1 = Label(paitents_frame, text="Name Label") 
paitents_label1.pack() 
paitents_label2 = Label(paitents_frame, text="Name Label") 
paitents_label2.pack() 

# prescription_frame with Labels in it 
prescription_frame = ttk.Frame(panewindow, width = 1600, height = 300, relief = RAISED) 
prescription_label1 = Label(prescription_frame, text="Prescription Text") 
prescription_label1.pack() 
prescription_label2 = Label(prescription_frame, text="Prescription Text") 
prescription_label2.pack() 

# Add the frames to panewindow 
panewindow.add(paitents_frame, weight = 1) 
panewindow.add(prescription_frame, weight = 1) 

root.mainloop() 

另一種選擇是完全離開了畫布,使用標籤來把圖像和文本框內。 See this post on how to use image in labels

相關問題