2017-01-06 4 views
0

私はtkinterで.gridを使用することにかなり新しいですし、どのようにl1変数の下にあるl2変数を得ることができるのだろうと思っていました。コードが実行されると、2番目のラベルがあまりにも遠すぎます(ウィンドウのサイズを変更する必要もあります)。私はそれを特定の場所(l1の下)に置くことができるようにしたいと思いますが、どうすればよいか分かりません。tkinterグリッドの使用

ありがとうございます。

例:

はログインしてください

ギャップがそこにも少し大きいです継続する

ようこそ。

現在のコード:

from tkinter import * 
from tkinter.ttk import * 
root = Tk() 
root.geometry("1300x720") 
myImage = PhotoImage(file='ssf.png') 
label = Label(image=myImage) 
label.grid(row=0) 
label.image = myImage 
l1 = Label(root, text="Welcome", font="Arial 100 bold", anchor="e").grid(row=0, column=1) 
l2 = Label(root, text="Please log-in to continue.", font="Arial 30 bold", anchor="e").grid(row=10, column=1) 

Preview of how it looks

答えて

0

さて、あなたはこのようにそれにテキストを配置するフレームウィジェットを使用することができます。

from tkinter import * 
from tkinter.ttk import * 
root = Tk() 
root.geometry("1300x720") 

myImage = PhotoImage(file='ssf.png') 
label = Label(image=myImage) 
label.grid(row=0) 
label.image = myImage 

labelFrame = Frame(root) 
labelFrame.grid(row=0,column=1) 

l1 = Label(labelFrame, text="Welcome", font="Arial 100 bold", anchor="e") 
l1.grid(row=0, column=0) 
l2 = Label(labelFrame, text="Please log-in to continue.", font="Arial 30 bold", anchor="e") 
l2.grid(row=1, column=0) 

フレームウィジェットは、行0であります列1に含まれ、 'l1'と 'l2'を含みます。

+0

ありがとう!しかし、私はどうすればそれを左に整列させるのだろうか? –

+0

あなたはもっと具体的になることができますか?あなたは正確に何を左に揃えたいですか? – zaka100

+0

おっと、私はラベルを左に若干左寄りにするために「ログインしてください」という意味です。青い矩形のすぐ隣にあり、後で追加することにしたウィジェットです。 –

0
  1. Pls以下の改訂版のコメントを参照してください。
  2. あなたのレイアウトを視覚化し、あなたのプログラムによって作成されたものと比較するために、紙にグリッドシステムを引き出すことをお勧めします。 フレームや ラベルなどのウィジェットの背景色を有効にすると、作成を視覚化するのに役立ちます。
  3. これらの参考文献を読むことをお勧めします。コーディングハッピー http://www.tkdocs.com/tutorial/grid.html http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/layout-mgt.html

。 :)

#from tkinter import * 
#from tkinter.ttk import * 
## I suggest you abbreviate the imported modules to help you keep track of which 
## module methods/functions you are using. See below. To be consistent, we will 
## use ttk widgets where possible. 

# Load tkinter for python 3 
import tkinter as tk 
import tkinter.ttk as ttk 

root = tk.Tk() 
root.geometry("1300x720") 

# Customise style of ttk widgets 
# I have added this to help you visualise the grid system. 
s=ttk.Style() 
s.configure('frame1.TFrame', background='pink') 
s.configure('l0.TLabel', background='blue') 
s.configure('l1.TLabel', background='green') 
s.configure('l2.TLabel', background='brown') 

# Create a frame inside root to contain all the widgets. 
# The frame contains a 2x2 grid. 
frame1 = ttk.Frame(root, style='frame1.TFrame', borderwidth=20, relief=tk.SUNKEN) 
frame1.grid(row=0, column=0, rowspan=2, columnspan=2, sticky='nsew') 

# Load Image 
# Added tk in fromt of PhotoImage. 
myImage = tk.PhotoImage(file='ssf.png') 

# Create a ttk.label to contain image 
## I added ttk in front of Label. If not, it will mean you will use tk.Label instead of ttk.Label. 
## Also I added frame1 as the 1st option to the ttk.Label to mean the ttk.Label 
## is inside frame1. 
## The "in_=frame1" option is added to grid to mean l0 is grid inside frame1 grid system. 
l0 = ttk.Label(frame1, image=myImage, width=500) 
l0.grid(in_=frame1, row=0, column=0, sticky='nsew') 
#label.image = myImage 

# Create a ttk.Label to contain l1 
l1 = ttk.Label(frame1, text="Welcome", style='l1.TLabel', font="Arial 100 bold", anchor=tk.E) 
l1.grid(in_=frame1, row=0, column=1) 
#l1 = Label(root, text="Welcome", font="Arial 100 bold", anchor="e").grid(row=0, column=1) 

# Create a ttk.Label to contain l2 
l2 = ttk.Label(frame1, text="Please log-in to continue.", style='l2.TLabel', font="Arial 30 bold") 
l2.grid(in_=frame1, row=1, column=1) 
#l2 = Label(root, text="Please log-in to continue.", font="Arial 30 bold", anchor="e").grid(row=10, column=1) 

# These configuration settings will let the grid columns and rows scale according 
# to the changing size of the Window i.e. root. 
root.rowconfigure(0, weight=1) 
root.columnconfigure(0, weight=1) 
frame1.rowconfigure(0, weight=1) 
frame1.columnconfigure(1, weight=1) 
関連する問題