为什么即使宽度相等,我的标签也会有不同的尺寸?

时间:2016-11-02 21:14:46

标签: python tkinter label

我有一个框架,位于row = 0,其中包含HighLow中我想要的文字column = 2column = 3 ##Forecast Frame self.ForecastFrame = Frame(self, bg='black') self.ForecastFrame.grid(row = 0, column = 0) ##Forecast Title self.forecastTitle = Frame(self.ForecastFrame, bg='white') self.forecastTitle.grid(row = 0, column = 0, sticky = E) self.forecastTitleHighLabel = Label(self.forecastTitle, text='High', font=('HelveticaNeue Light', 12), fg='white', bg='green', width = '7', anchor='center') self.forecastTitleHighLabel.grid(row = 0, column = 2, sticky = E) self.forecastTitleLowLabel = Label(self.forecastTitle, text='Low', font=('HelveticaNeue Light', 12), fg='white', bg='blue', width = '7', anchor='center') self.forecastTitleLowLabel.grid(row = 0, column = 3, sticky = E) ##Forecast One Labels self.forecastOneDate = '' self.forecastOneIcon = '' self.forecastOneHigh = '' self.forecastOneLow = '' self.forecastOne = Frame(self.ForecastFrame, bg='black') self.forecastOne.grid(row = 1, column = 0) self.forecastOneDateLabel = Label(self.forecastOne, font=('HelveticaNeue Light', 12), fg='white', bg='yellow', width=10, anchor='w') self.forecastOneDateLabel.grid(row = 0, column = 0, sticky = W) self.forecastOneIconLabel = Label(self.forecastOne, bg='red', width=50) self.forecastOneIconLabel.grid(row = 0, column = 1, sticky = W) self.forecastOneHighLabel = Label(self.forecastOne, font=('HelveticaNeue Light', 12, 'bold'), fg='white', bg='blue', width = '7', anchor='center') self.forecastOneHighLabel.grid(row = 0, column = 2, sticky = E) self.forecastOneLowLabel = Label(self.forecastOne, font=('HelveticaNeue Light', 12, 'bold'), fg='white', bg='green', width = '7', anchor='center') self.forecastOneLowLabel.grid(row = 0, column = 3, sticky = E) 。我下面有另一行包含数值。但是在标签上我将宽度设置为全等于7.

我在这里做错了什么?

<nav class="navbar navbar-default custom-nav">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#"></a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">About <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">History</a></li>
            <li><a href="team.php">Meet The Team</a></li>
            <li><a href="#">Facilities</a></li>
            <li><a href="#">Opening Times</a></li>
            <li><a href="membership.php">Membership</a></li>
          </ul>
        </li>
        <li><a href="#">Diary</a></li>
        <li><a href="lessons.php">Lessons</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> Events<span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="corporate.php">Corporate Days </a></li>
            <li><a href="hens_stags.php">Hens & Stags</a></li>
            <li><a href="#">Group Bookings</a></li>
          </ul>
        </li>
        <li><a href="#">Gallary</a>
        </li>
        <li><a href="contact.php">Contact</a>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>

1 个答案:

答案 0 :(得分:1)

effbot.org:Label

  

width =

     

标签的宽度。如果标签显示文字,则尺寸以文本单位给出。如果标签显示图像,则尺寸以像素(或屏幕单位)给出。如果大小设置为0或省略,则根据标签内容计算。 (宽度/宽度)

这意味着width取决于字体大小和重量。

import tkinter as tk

root = tk.Tk()


l1 = tk.Label(root, text='Hello', width=7, fg='white', bg='blue')

f = ('HelveticaNeue Light', 12)

l2 = tk.Label(root, text='Hello', width=7, fg='white', bg='green', font=f)

f = ('HelveticaNeue Light', 12, 'bold')

l3 = tk.Label(root, text='Hello', width=7, fg='white', bg='red', font=f)


l1.grid()
l2.grid()
l3.grid()

root.mainloop()

enter image description here