2016-04-03 132 views
-1

これは私の現在のコードで、ウィンドウに表示されますが、文字通り入力したとおりに印刷されます(明らかに)。しかし、これらを書く方法があるので、彼らは力/指数として表示されますか? 「Pythonの上付き文字をGUIで書く方法はありますか?

questionNum = 0 

#SET QUESTIONS HERE 

questions = [0,"Given that y = x^4 + 6x^1/2, find dy/dx","Write down the value of 125^1/3","Fully factorise 9x^2 - 25","Find the value of 8^5/3"] 

answer1 = [0,"4x^4 + 3x^1/2","25","(3x-5)(3x+5)","1/2"] 

answer2 = [0,"4x^3 + 6x^1/2","1/5","(3x+5)(3x+5)","Answer 2 (4)"] 

answer3 = [0,"4x^3 + 6x^-1/2","5","(3x-5)(3x-5)","Answer 3 (4)"] 

answer4 = [0,"4x^3 + 1/2x^-3/2","125","(9x+5)(x-5)","Answer 4 (4)"] 

これは、それがどのように見えるかです:私はちょうどあなたがいずれかを必要とする場合には残りの部分を追加しました。これは、変更される必要があるビットである

this is what it looks like

rightanswers = [0,1,2,3,4] #These are the correct answers 
" 

。何が起こっているのかを確認する:

from tkinter import * #The TKinter module imported in order to create the menus 
import sys #Invoke functions and statements - allows operations to work and searches for named modules i.e. TKinter 
global questionNum #Connections between functinos and arrays 

questionNum = 0 
#SET QUESTIONS HERE 
questions = [0,"Given that y = x^4 + 6x^1/2, find dy/dx","Write down the value of 125^1/3","Fully factorise 9x^2 - 25","Find the value of 8^5/3"] #This is the question bank which has been set up as an array 
answer1 = [0,"4x^4 + 3x^1/2","25","(3x-5)(3x+5)","1/2"] #These are the possible answers - this is where multiple choice answers would go 
answer2 = [0,"4x^3 + 6x^1/2","1/5","(3x+5)(3x+5)","Answer 2 (4)"] 
answer3 = [0,"4x^3 + 6x^-1/2","5","(3x-5)(3x-5)","Answer 3 (4)"] 
answer4 = [0,"4x^3 + 1/2x^-3/2","125","(9x+5)(x-5)","Answer 4 (4)"] 
rightanswers = [0,1,2,3,4] #These are the correct answers 

normal = Tk() #Build the TKinter 
normal.geometry("850x350") #Set the size of the normal chess form to 850x350 
normal.title("Normal Chess") #Sets a title for the normal chess form 
normal.columnconfigure(1,minsize = 300) #This is whereabouts the question and answers are displayed 
questionVar = StringVar() #Question will be a string 
answerVar = IntVar() #Answers will be numbers 
answerVar.set(1) #Questions can only have one solution 
aText = StringVar() #Text for the answers - options 
bText = StringVar() 
cText = StringVar() 
dText = StringVar() 
qNoText = StringVar() #Questions text which is displayed to the user 
questionText = StringVar() 
normal.withdraw() #Allows the user to only select one option 

title = Tk() #Builds the TKinter 
title.title("Chess") #Title of the form 
title.geometry("300x300") #The size of the image 
watermark = PhotoImage(file = "watermark.gif") #Link to the image itself 
Label(image = watermark).place(x=0,y=0,relwidth=1,relheight=1) #Places the image onto the form set on the x,y coordinates 
title.withdraw() 



menu = Tk() #Builds the TKinter 
menu.title("Main Menu") #Displays 'Main Menu' on screen 
menu.geometry("400x350") #Size of the form 
p1var = StringVar() #Sets a variable for Player 1 and Player 2 
p2var = StringVar() 


def Quit(): #Quit function - allows the user to exit the program 
    menu.destroy() 
    title.destroy() 
    normal.destroy() 
    sys.exit() #Gets rid of all the modules 

def play(): 
    title.deiconify() 
    Label(title,text = "Enter Details:",font = ("Calibri",25)).grid(row = 1, column = 1) 
    #'Enter Details' font, size and location on the form 
    Label(title,text = "CHESS",font = ("Calibri",50)).grid(row = 0,column = 1) 
    #'CHESS' font, size and location on the form 
    Label(title, text = "Player 1: ").grid(row = 2,column = 0) 
    #'Player 1' font, size and location on the form 
    Entry(title,textvariable = p1var,width = 30).grid(row = 2,column = 1) 
    #Allows the user to input a string for the player 1 name 
    Label(title, text = "Player 2: ").grid(row = 3,column = 0) 
    #'Player 2' font, size and location on the form 
    Entry(title,textvariable = p2var,width = 30).grid(row = 3,column = 1) 
    #Allows the user to input a string for the player 2 name 
    Label(title,text = "Select Game Mode: ").grid(row = 4,column = 1) 
    #'Select Game Mode: ' font, size and location on the form 
    Button(title,command = lambda: playNormalChess(p1var.get(),p2var.get()),text = "Normal Chess").grid(row = 6,column = 1,sticky = W) 
    #Button for normal chess 
    Button(title,command = lambda: playSpeedChess(p1var.get(),p2var.get()),text = "Speed Chess").grid(row = 6,column = 1,sticky = E) 
    #Button for speed chess 
    Button(title,command = instructions,text = "Instructions").grid(row = 7,column = 1,pady =10) 
    #Button for instructions 

def playNormalChess(p1,p2): 
    normal.deiconify() 
    t = Label(textvariable = qNoText,font = ("Arial",50)).grid(row = 0,column = 1,sticky = W,columnspan=2,padx = 20,pady =10) 
    #Functions for selection 
    q =Label(textvariable = questionVar,font = ("Arial",30)).grid(row = 1,column = 1,sticky = W,columnspan=2,padx = 20,pady =10) 
    #Font and size the questions are set in 
    a=Radiobutton(variable = answerVar,value = 1,textvariable = aText,font = ("Arial",18)).grid(row = 3,column = 1,pady =5,padx = 20,sticky = W) 
    #These are the radio buttons which are linked to the answers - set the font and the size of the text 
    b=Radiobutton(variable = answerVar,value = 2,textvariable = bText,font = ("Arial",18)).grid(row = 4,column = 1,padx = 20,sticky = W) 
    c=Radiobutton(variable = answerVar,value = 3,textvariable = cText,font = ("Arial",18)).grid(row =5,column = 1,pady = 5,padx = 20,sticky = W) 
    d=Radiobutton(variable = answerVar,value=4,textvariable = dText,font = ("Arial",18)).grid(row = 6,column = 1,padx = 20,sticky = W) 
    Button(text = "Submit",command = confirm).grid(row =0,column = 3,sticky = W,pady = 10) #Submit button to confirm their answers 
    newQuestion() #Calls the function for a new question 

def confirm(): 
    if messagebox.askyesno('Confirm','Are You Sure?'): #This is the conformation of the correct answer for the user 
     try: 
      if answerVar.get() == rightanswers[questionNum]: #If they select yes to confirm they are happy with the selection 
      # - it checks their answer with the correct answer and prints 'Right' or 'Wrong' depending on their answer 
       print("Right") 
      else: 
       print("Wrong") 

      newQuestion() #Once their question is answered, a new question will be presented to them 
     except IndexError: 
      print("No more Questions") 


def newQuestion(): 
    global questionNum 
    questionNum = questionNum + 1 

    try: 
     qNoText.set("Question "+str(questionNum)) 
     aText.set(answer1[questionNum]) 
     bText.set(answer2[questionNum]) 
     cText.set(answer3[questionNum]) 
     dText.set(answer4[questionNum]) 
     questionVar.set(questions[questionNum]) 
    except IndexError: 
     print("No more questions") 


Label(menu,text = "AS Maths Chess",font = ("Arial",37)).pack(pady = 20) 
Button(menu,text = "Play",background = "black",foreground = "white",font = ("Arial",20),command = play).pack(ipadx = 80,ipady = 20, pady = 10) 
Button(menu,text = "Quit",font = ("Arial",20),command = Quit).pack(ipadx = 80,ipady = 20, pady = 10) 
+1

関連:http://stackoverflow.com/questions/13875507/convert-numeric-strings-to-superscript/13875688#13875688 – Gerrat

+0

これは、9年前に依頼されました:http://stackoverflow.com/questions/17768347/canvas-object-in-tkinterの式を表現する 私の提案:Tkinterを忘れ、Gtk、wxWidgets、またはQtを使用する – saeedgnu

+1

@saeedgnu:なぜtkinterを忘れるのですか? Tkinterは、特にGUIプログラミングの仕方を学ぶための素晴らしいツールキットです。 –

答えて

1

Unicodeには上付き数字が付きますs、算術演算子、およびBMPの一部の文字(tcl/tkでサポートされています)があります。 Wikipediaを参照してください。これらの制約の中で、サポートされているフォントを指定すると、答えは「はい」です。 Windows上では、IDLEから実行すると、次のようにうまく動作し、うまく見えますが、私はtkフォントを大きくして、上付き文字を簡単に見たいと思っています。

import tkinter as tk 
root = tk.Tk() 
# "4x^4 + 3x^1/2" 
t1 = "4x\u2074 + 3x\u207b\u00b2" # \u excapes, if needed 
t2 = "4x⁴ + 3x⁻²" 
label = tk.Label(root, text=t2) 
label.pack() 
+0

指数分数のUnicodeコード??私はwikiによって提供されたものを使ってみましたが、分数よりもむしろ 'U + 00B9'を出力します。他のものは動作します。 – Inderbir

+0

'U + ####'または 'U + ########'は、UnicodeコードポイントのUnicode標準の表記法です。 '\ u ####'と '\ U ########'はPythonの文字列エスケープ表記で、 '\ n'や' \ x ## 'のようなかなり一般的なバックスラッシュ表記に基づいています。 –

関連する問題