2015-10-22 139 views
0

因此,我在Tkinter GUI和一些Google搜索上跟踪了几个网络研讨会之后,我完成了一项任务,制作了一个小型Bingo应用程序(Windows btw)。tkinter标签不显示

我已经取得了成功的小应用程序正常工作,但这次我试着用我认为的知识做到这一点。

无论如何,在Bingo应用程序中,我需要50个标签,代表宾果卡的50个数字,5个10行的数字。 我知道如何排列它们,但它们需要先显示出来。

你可以看看我的代码并评估我做错了什么吗?

忽略荷兰许多意见:)

''' 
     iWMMMM0. 
    XM#o:..iIMM. 
    MM. :XC17, YM8 
    [email protected] [email protected] .W# ,M, 
#M [email protected] ,0$: M0 MM 
.M. @7 [email protected], ME MQ 
MM iM. #$Ci$Q ;M#CCY:. 
CM ,M, b#$; v;[email protected]@1 
MQ WQ .tE0Q2nCCYc;i,:7#MMi 
:M @b . .. .:YCb0U: YMM. 
    WM [email protected] .... ,i:  ,b#; bMQ,SbW06i 
    oM MC ... MME9#$Y . .#b [email protected]@MM1 
    Mi @7 ... @M .v iMt ... @X :CoUzt: :MM,   [email protected] :nQ#$Ui 
    M: @Y .. M9 [email protected] b# ... Zb1#Zi. .;Z#Y SMi  MM#t;vCMM,CMM0C77bMM8 
    M, @7 .. @6 Y7 ;Mn ... #QB.  ,#b ZM  MM .n9U; bMM. ;z2U7 MM 
    M: @Y ., @9.;tQQv ... ;MQ,   :@; Mv MM X$: M; , ;$2. [email protected] M8 
    M: @7 .. #BW1i ..,:Y$Mt$   MX MMtiEM: [email protected]: ZME,.W$c ; [email protected] .M 
    M: @Y ., ME .. .ZMM8o89   QM. i;[email protected]@ .Zb7: ,BBYzQ [email protected] Mi M 
    Mi @X .. M, [email protected]  :bWQb,  ,#M1;IoIX [email protected] bMMX iM [email protected]$CM M, M 
    .MM @7 ., Mn EM8AEE#: . v$AWQ#$Q#MMIWz, ;$Z$ MMzW2 [email protected] $ZY$C bM CM 
    MZ [email protected] .. @@. ;t [email protected] ., CWUWnE$b7: :Y #$c MEYY .MM8 :; vM. M1 
iM iMi .., #bWI,[email protected]#C .., Mv i#E. UMM2 @BC oC;tMMCES [email protected] MM 
    MY E0 ... [email protected]:Z$MZ .Mv ... M8 7WEz MnZo @obUA: ic.7bEW$#[email protected] iMM 
    CMi [email protected]; .. b#;  $$ ... ,MQ 7$Zz @zbz @IXW#E69z. 80 ::iAMM; 
    iM$ .$Qi . :Z$Q0Q#6. ... MQ0 C$b1 MtQ7 YZWW,[email protected]@ MC [email protected] 
    MM7 iBWv.  ..,  .M#7$ v$8; vQ0:iCAM: :#bM0 nMU BM 
     :MM1 .6$Q7:   ;MMS:Q7 @#i12EMzEESB# .7:,,[email protected] MM 
     ,MM#: ,[email protected]; [email protected]@B$72n7i.,vo tQQIZQ$WC .#MQ 
      [email protected];,,:ivY;i,,iEMM...,,:iC2Q#MMMQ$M$..,i::,;9MMZ 
       YIEbEAU269086, ;Bb08bAz;:.  7QbEZb8boi 
''' 

##Basale geimporteerde functies 
from tkinter import* 
#self=() 
#Importeert de randomfucntie 
#import random 
#Importeert de Python Debugger 
#Runnen met pdb.run 
#import pdb 
#Importeert de PIL Python Image Library 
#from PIL import Image, ImageTK 
#image = image.open("bestandsnaam.jpg") 
#photo ImageTK.PhotoImage(image) 
#Een afbeelding op een label 
#label = Label(image=photo(als photo de naam van de variable is)) 
#label.image = photo (bewaren als referentie?<- geen idee nog) 


''' 
#=# Start van metainfo class/klasse 
class MetaBingPy(): 

    def __init__(self): 
     super(MetaBingPy, self).__init__() 
     self.parent = parent 
     self.pack() 
     MetaBingPy.BingoPyC(self) 
##Functie voor random number generator 

def BingoPyC: 
    self.root = Tk() 
    self.root.title("Python Bingo") 

#=#Einde metainfo class 
''' 



###def random.randint(1,50) 
#####of# randrange(1,50) 

##GUI voor BingPy, moet root heten. Geen eigen naam, zoals: 
##BingPyGUI wat ik had. 
root = Tk() 
root.wm_title("Bingo Python") 
root.wm_maxsize("800","600") 
root.wm_minsize("800","600") 
#root.wm_grid(baseWidth="800", baseHeight="800") 

#root.grid("800x800") 
#_tkinter.TclError: wrong # args: should be "wm grid window ?baseWidth baseHeight 
#widthInc heightInc?" 
##self.title="Bingo Python" 
###GUI voor BingPy code, altijd afsluiten met: 
###bovenaan 
root.mainloop() 



''' 
#Algemene Python Bingo klasse 
class BingoPy 

def 

''' 



#Labels voor alle nummers (50) 

label1 = Label(root, text="1jghjgjkhjhg") 
label1.grid() 
label1.pack() 

label2 = Label(root, text="2") 
label2.grid() 
label2.pack() 

label3 = Label(root, text="3") 
label3.grid() 
label3.pack() 

label4 = Label(root, text="4") 
label4.grid() 
label4.pack() 

label5 = Label(root, text="5") 
label5.grid() 
label5.pack() 

label6 = Label(root, text="6") 
label6.grid() 
label6.pack() 

label7 = Label(root, text="7") 
label7.grid() 
label7.pack() 

label8 = Label(root, text="8") 
label8.grid() 
label8.pack() 

label9 = Label(root, text="9") 
label9.grid() 
label9.pack() 

label10 = Label(root, text="10") 
label10.grid() 
label10.pack() 

label11 = Label(root, text="11") 
label11.grid() 
label11.pack() 

label12 = Label(root, text="12") 
label12.grid() 
label12.pack() 

Label13 = Label(root, text="13") 
Label13.grid() 
Label13.pack() 

label14 = Label(root, text="14") 
label14.grid() 
label14.pack() 

label15 = Label(root, text="15") 
label15.grid() 
label15.pack() 

label16 = Label(root, text="16") 
label16.grid() 
label16.pack() 

label7 = Label(root, text="17") 
label17.grid() 
label17.pack() 

label18 = Label(root, text="18") 
label18.grid() 
label18.pack() 

label19 = Label(root, text="19") 
label19.grid() 
label19.pack() 

label20 = Label(root, text="20") 
label20.grid() 
label20.pack() 

label21 = Label(root, text="21") 
label21.grid() 
label21.pack() 

label22 = Label(root, text='22') 
label22.grid() 
label22.pack() 

label23 = Label(root, text="23") 
label23.grid() 
label23.pack() 

label24 = Label(root, text="24") 
label24.grid() 
label24.pack() 

label25 = Label(root, text="25") 
label25.grid() 
label25.pack() 

label26 = Label(root, text="26") 
label26.grid() 
label26.pack() 

label27 = Label(root, text="27") 
label27.grid() 
label27.pack() 

label28.Label(root, text="28") 
label28.grid() 
label28.pack() 

label29 = Label(root, text="29") 
label29.grid() 
label29.pack() 

label30 = Label(root, text="30") 
label30.grid() 
label30.pack() 

label31 = Label(root, text="31") 
label31.grid() 
label31.pack() 

label32 = Label(root, text="32") 
label32.grid() 
label32.pack() 

label33 = Label(root, text="33") 
label33.grid() 
label33.pack() 

label34 = Label(root, text="34") 
label34.grid() 
label34.pack() 

label35 = Label(root, text="35") 
label35.grid() 
label35.pack() 

label36 = Label(root, text="36") 
label36.grid() 
label36.pack() 

label37 = Label(root, text="37") 
label37.grid() 
label37.pack() 

label38 = Label(root, text="38") 
label38.grid() 
label38.pack() 

label39 = Label(root, text="39") 
label39.grid() 
label39.pack() 

label40 = Label(root, text="40") 
label40.grid() 
label40.pack() 

label41 = Label(root, text="41") 
label41.grid() 
label41.pack() 

label42 = Label(root, text="42") 
label42.grid() 
label42.pack() 

label43 = Label(root, text="43") 
label43.grid() 
label43.pack() 

label44 = Label(root, text="44") 
label44.grid() 
label44.pack() 

label45 = Label(root, text="45") 
label45.grid() 
label45.pack() 

label46 = Label(root, text="46") 
label46.grid() 
label46.pack() 

label47 = Label(root, text="47") 
label47.grid() 
label47.pack() 

label48 = Label(root, text="48") 
label48.grid() 
label48.pack() 

label49 = Label(root, text="49") 
label49.grid() 
label49.pack() 

label50 = Label(root, text="50") 
label50.grid() 
label50.pack() 



#Maakt het rood (en als het mogelijk is: doorstrepen) als het getrokken is 

#Waarde-return in veld + niet meer zelfde nummer kunnen kiezen 


#-------------------------------------------------------------------------- 
#Knoppen voor afsluiten en nieuw getal 

##Afsluiten 
#def bingoclose(): 
# print("Bingo Afsluiten") 
''' 
bAfsluiten = Button(root,text"Sluit Bingo Af") 
bAfsluiten.pack() 
''' 

''' 
{ 
'title' : ['BingPy'], 
'summary' : ['Simple Bingo Python Application with rand.num generator'], 
'authors' : ['Thomas'] 
#'date' : ['2015'] 
'base_url' : ['http://www.rainydays.eu'] 
} 

''' 

''' 
Info en weblinks: 
https://docs.python.org/3/library/random.html?highlight=random#module-random 
http://infohost.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf (Python 2.7!) 
https://wiki.scinet.utoronto.ca/wiki/images/4/41/Pwcgui.pdf 
http://stackoverflow.com/questions/3643235/how-to-add-a-margin-to-a-tkinter 
-window Margins in Python (padx, pady) 

''' 
+0

为了更轻松,更快地代码化代码,请删除所有评论和评论部分。正如你所看到的,你必须滚动很多,直到你看到一些“有用”的代码。 –

回答

0
  1. 您需要了解的数据结构,像list
  2. 您在代码早期有root.mainloop()。这开始一个循环。之后没有任何事情会被执行,直到窗口关闭,这意味着它必须在最后。
  3. 选择一个几何管理器。您不能在同一个父窗口小部件中使用grid()pack(),更不能在同一个子窗口小部件上使用。
  4. 您有一些拼写错误,如label7 =而不是label17 =label28.Label而不是label28 = Label。如果您使用了list,则绝大多数情况不会发生。
  5. 显然,50个标签不适合放在该尺寸窗口中的单个列中。您在涉及窗口大小,标签放置,可能的滚动条等方面做出了一些决定。

考虑到所有这些点(最后除外)的结果代码如下所示。

from tkinter import * 

root = Tk() 
root.wm_title("Bingo Python") 
root.wm_maxsize("800","600") 
root.wm_minsize("800","600") 

labels = [Label(root, text=i) for i in range(1, 51)] 
for label in labels: 
    label.pack() 

root.mainloop() 
+0

谢谢,网格和标签的数量确实搞砸了。 我稍微调整你的范围数量为1,11/12,21,以完成我的需要。 非常感谢! :) 仍在学习如何使用,但我已评级此评论 –