<p>Hello all,</p>
<p>I just started learning pyqt and it's been nice.. When i'm experimenting with the
tabbar i encountered this.. As a minimal example i want to show a
button in tab1 and a label in tab2.. Here's what i did</p><pre class="prettyprint"><code><span class="kwd">from</span><span class="pln"> </span><span class="typ">PyQt4</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pln"> <br>
</span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Ui_TabWidget</span><span class="pun">(</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QTabWidget</span><span class="pun">):</span><span class="pln"> <br>
</span><span class="kwd">def</span><span class="pln"> __init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln">parent</span><span class="pun">=</span><span class="kwd">None</span><span class="pun">):</span><span class="pln"><br>
</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QTabWidget</span><span class="pun">.</span><span class="pln">__init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln">parent</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setObjectName</span><span class="pun">(</span><span class="str">"TabWidget"</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">resize</span><span class="pun">(</span><span class="lit">400</span><span class="pun">,</span><span class="pln"> </span><span class="lit">300</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setWindowTitle</span><span class="pun">(</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QApplication</span><span class="pun">.</span><span class="pln">translate</span><span class="pun">(</span><span class="str">"TabWidget"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"TabWidget"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">None</span><span class="pun">,</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QApplication</span><span class="pun">.</span><span class="typ">UnicodeUTF8</span><span class="pun">))</span><span class="pln"><br>
<br> </span><span class="com">#Creating the tabbar</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">=</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QTabBar</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="com">#Adding the first tab</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">.</span><span class="pln">addTab</span><span class="pun">(</span><span class="str">"tab1"</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">.</span><span class="pln">setTabText</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="str">"TAB1"</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="com">#The widget intended for tab1</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetforTab1</span><span class="pun">=</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QWidget</span><span class="pun">()</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">addTab</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetforTab1</span><span class="pun">,</span><span class="str">""</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">buttonForTab1</span><span class="pun">=</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QPushButton</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetforTab1</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">buttonForTab1</span><span class="pun">.</span><span class="pln">setText</span><span class="pun">(</span><span class="str">"Button in Tab1"</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="com">#Adding the second Tab</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">.</span><span class="pln">addTab</span><span class="pun">(</span><span class="str">"tab2"</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">.</span><span class="pln">setTabText</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="str">"TAB2"</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="com">#The widget intended for tab2</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetForTab2</span><span class="pun">=</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QWidget</span><span class="pun">()</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">addTab</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetForTab2</span><span class="pun">,</span><span class="str">""</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">labelForTab2</span><span class="pun">=</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QLabel</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetForTab2</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">labelForTab2</span><span class="pun">.</span><span class="pln">setText</span><span class="pun">(</span><span class="str">"Label in Tab2"</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="com">#Adding the tabbar to the tabwidget</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setTabBar</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">.</span><span class="pln">setMovable</span><span class="pun">(</span><span class="kwd">True</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setCurrentIndex</span><span class="pun">(</span><span class="lit">0</span><span class="pun">)</span><span class="pln"><br><br></span><span class="kwd">if</span><span class="pln"> __name__ </span><span class="pun">==</span><span class="pln"> </span><span class="str">"__main__"</span><span class="pun">:</span><span class="pln"><br>
</span><span class="kwd">import</span><span class="pln"> sys<br> app </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QApplication</span><span class="pun">(</span><span class="pln">sys</span><span class="pun">.</span><span class="pln">argv</span><span class="pun">)</span><span class="pln"><br>
ui </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Ui_TabWidget</span><span class="pun">()</span><span class="pln"><br> ui</span><span class="pun">.</span><span class="pln">show</span><span class="pun">()</span><span class="pln"><br>
sys</span><span class="pun">.</span><span class="kwd">exit</span><span class="pun">(</span><span class="pln">app</span><span class="pun">.</span><span class="pln">exec_</span><span class="pun">())<br><br></span></code></pre>
<p>In the above program the widget intended for tab1 and tab2
functioned well. Anyhow i can't see a connection between the widget and
the tabbar.. The tabbar tab is created independently and so the
tabwidget tab. Both have tab titles the one given for tabbar alone is
shown.. But if i set the index as o the first tab is shown along with
widgetForTab1.. </p>
<p>In my second program the lack of coupling between the widget and tabbar is the cause for the problem.. <br></p><pre class="prettyprint"><code><span class="kwd">from</span><span class="pln"> </span><span class="typ">PyQt4</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pln"> <br>
</span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Ui_TabWidget</span><span class="pun">(</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QTabWidget</span><span class="pun">):</span><span class="pln"><br>
</span><span class="kwd">def</span><span class="pln"> __init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln">parent</span><span class="pun">=</span><span class="kwd">None</span><span class="pun">):</span><span class="pln"><br>
</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QTabWidget</span><span class="pun">.</span><span class="pln">__init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln">parent</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setObjectName</span><span class="pun">(</span><span class="str">"TabWidget"</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">resize</span><span class="pun">(</span><span class="lit">400</span><span class="pun">,</span><span class="pln"> </span><span class="lit">300</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setWindowTitle</span><span class="pun">(</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QApplication</span><span class="pun">.</span><span class="pln">translate</span><span class="pun">(</span><span class="str">"TabWidget"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"TabWidget"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">None</span><span class="pun">,</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QApplication</span><span class="pun">.</span><span class="typ">UnicodeUTF8</span><span class="pun">))</span><span class="pln"><br>
<br> </span><span class="com">#Creating the tabbar</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">=</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QTabBar</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="com">#Adding the first tab</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">.</span><span class="pln">addTab</span><span class="pun">(</span><span class="str">"tab1"</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">.</span><span class="pln">setTabText</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="str">"TAB1"</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="com">#Adding the second Tab</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">.</span><span class="pln">addTab</span><span class="pun">(</span><span class="str">"tab2"</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">.</span><span class="pln">setTabText</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="str">"TAB2"</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">.</span><span class="pln">setMovable</span><span class="pun">(</span><span class="kwd">True</span><span class="pun">)</span><span class="pln"><br>
</span><span class="com">#Adding the tabbar to the tabwidget</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setTabBar</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">tabBar</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="com">#The widget intended for tab1</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetforTab1</span><span class="pun">=</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QWidget</span><span class="pun">()</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">addTab</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetforTab1</span><span class="pun">,</span><span class="str">""</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">buttonForTab1</span><span class="pun">=</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QPushButton</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetforTab1</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">buttonForTab1</span><span class="pun">.</span><span class="pln">setText</span><span class="pun">(</span><span class="str">"Button in Tab1"</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="com">#The widget intended for tab2</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetForTab2</span><span class="pun">=</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QWidget</span><span class="pun">()</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">addTab</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetForTab2</span><span class="pun">,</span><span class="str">""</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">labelForTab2</span><span class="pun">=</span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QLabel</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">widgetForTab2</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">labelForTab2</span><span class="pun">.</span><span class="pln">setText</span><span class="pun">(</span><span class="str">"Label in Tab2"</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setCurrentIndex</span><span class="pun">(</span><span class="lit">0</span><span class="pun">)</span><span class="pln"><br><br></span><span class="kwd">if</span><span class="pln"> __name__ </span><span class="pun">==</span><span class="pln"> </span><span class="str">"__main__"</span><span class="pun">:</span><span class="pln"><br>
</span><span class="kwd">import</span><span class="pln"> sys<br> app </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QtGui</span><span class="pun">.</span><span class="typ">QApplication</span><span class="pun">(</span><span class="pln">sys</span><span class="pun">.</span><span class="pln">argv</span><span class="pun">)</span><span class="pln"><br>
ui </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Ui_TabWidget</span><span class="pun">()</span><span class="pln"><br> ui</span><span class="pun">.</span><span class="pln">show</span><span class="pun">()</span><span class="pln"><br>
sys</span><span class="pun">.</span><span class="kwd">exit</span><span class="pun">(</span><span class="pln">app</span><span class="pun">.</span><span class="pln">exec_</span><span class="pun">())</span><span class="pln"><br>
</span></code></pre><pre class="prettyprint">The output for the second program was horrible.. I got four with first<br>two tabs with no tab text and the tabs had Button in Tab1,Label in<br>Tab2,Label in Tab2 and Label in Tab2 respectively.. Can you tell me why<br>
this is happening??? What should i do to solve this problem???</pre>