これはpythonでTkinterの個人的な練習/テストとしてコーディングしていますが、辞書内に辞書を含むファイルを読み込もうとしています問題ではないはずのものを読んでください。ただし、問題は、TempDictが定義どおりに分類されていないことです。Tkinter Python、ファイルからロードされた辞書から文字列を読み込もうとしています
コードの一部が正しくインデントされていないと、非常にうまくコピー&ペーストできないため、私はいくつかの修正を加えなければなりません。あなたは引数を指定してDictFromFile()
を実行することはできません
tk.Label(self, text=DictFromFile(TempDict[QuizSelection(Topic, DifficultyInput)]["Question1"])
に間違った方法でDictFromFile()
を使用
import tkinter as tk
from tkinter import *
from tkinter import ttk
import tkinter.messagebox as tm
import os
import pickle
class Quiz (tk.Tk):
def __init__ (self, *args , **kwargs):
tk.Tk.__init__(self, *args , **kwargs)
tk.Tk.wm_title(self, "Quiz")
container = tk.Frame (self)
container.pack (side = "top" , fill = "both" , expand = True)
container.grid_rowconfigure (0,weight = 1)
container.grid_columnconfigure (0,weight = 1)
self.frames = {}
for F in (StartPage, Menu, Difficulty, QuestionsStart):
frame = F(container, self)
self.frames[F] = frame
frame.grid (row = 0, column = 0 , sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text = "Login")
label.pack(pady = 10 , padx = 10)
global Username
User = tk.Label (self, text = "Username")
User.pack()
Username = tk.Entry(self)
Username.pack()
Pass = tk.Label (self, text = "Password")
Pass.pack()
Password = tk.Entry (self, show = "*")
Password.pack()
button1 = ttk.Button(self, text = "Login",
command = lambda: Login(Username,Password,parent,controller,self))
button1.pack()
button2 = ttk.Button(self, text = "Sign Up",
command = lambda: Signup())
button2.pack()
class Menu(tk.Frame):
def __init__ (self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = "Menu")
label.pack()
Label = tk.Label(self, text = "Please enter Maths or Chemsitry")
Label.pack()
Topic = tk.Entry(self)
Topic.pack()
Proceed = ttk.Button(self, text = "Proceed", command = lambda: controller.show_frame(Difficulty))
Proceed.pack()
Result = ttk.Button(self, text = "Results",
command = lambda: Results(controller))
Result.pack()
Logout = ttk.Button(self, text = "Log Out",
command = lambda: controller.show_frame(StartPage))
Logout.pack()
class Difficulty(tk.Frame):
def __init__ (self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text = "Difficulty")
label.pack()
Label = tk.Label(self, text = "Please enter Easy, Medium or Hard")
Label.pack()
DifficultyInput = tk.Entry(self)
DifficultyInput.pack()
StartQuiz = tk.Button(self , text = "Start Quiz", command = lambda: controller.show_frame(QuestionsStart))
StartQuiz.pack()
backtomenu = ttk.Button(self, text = "Back to Menu",
command = lambda: controller.show_frame(Menu))
backtomenu.pack()
def DictFromFile():
TempDict = {}
try:
with open("Questions.txt" , "r") as file:
TempDict = eval(file.read())
file.close()
except IOError as error:
print (error)
print(TempDict)
return TempDict
DictFromFile()
def QuizType(Topic, DifficultyInput):
QuizSelection = str(Topic.get().lower() + DifficultyInput.get().lower())
return QuizSelection
class QuestionsStart (tk.Frame):
def __init__(self , parent, controller):
tk.Frame.__init__(self,parent)
Label = tk.Label(self, text = DictFromFile(TempDict[QuizSelection(Topic, DifficultyInput)]["Question1"]))
Label.pack()
def Login(Username,Password,parent,controller,self):
Usernames = []
Login = True
Username = Username.get()
Password = Password.get()
try:
with open ("Usernames&Passwords.txt" , "rb") as file:
for each in pickle.load(file):
Usernames.append(each.strip("\n"))
file.close()
except IOError as error:
print (error)
for each in range(len(Usernames)):
if Usernames[each] == Username :
if Usernames[each + 1] == Password:
Login = True
controller.show_frame(Menu)
break
else:
Login = False
else:
Login = False
if Login == False:
tm.showinfo("Your Username or Password is incorrect" , "Your Usename or Password is incorrect")
def Results (controller):
UsersResults = []
Counter = 0
Topic = []
Difficulty = []
Mark = []
Percentage = []
Grade = []
try:
with open(Username.get() + ".txt" , "r" , encoding = "UTF-8") as file:
for each in file:
UsersResults.append(each.strip("\n"))
except IOError as error:
print(error)
print (UsersResults)
print (Counter)
for each in range (len(UsersResults)):
Topic = UsersResults[Counter]
Difficulty = UsersResults[Counter + 1]
Mark = UsersResults[Counter + 2]
Percentage = UsersResults[Counter + 3]
Grade = UsersResults[Counter + 4]
Counter += 5
return UsersResults
app = Quiz()
app.geometry ("500x300")
app.mainloop()
[辞書が含まれているメモ帳] [1] [Iが取得されているエラー] [2] [ユーザー名]と[パスワード] [3] [1]:https://gyazo.com/8ea5faab3027411927762305535cb0c3 [2]:https://gyazo.com/2d6a5d659020ee5aa259b61f5cbf3c08 [3]:https://gyazo.com/1f7f87a17ce5749fa92e717fc6327035 –
ご質問には可能な限り短い例のデータを含めてください。誰もあなたのコードをテストするためだけにデータをダウンロードするリンクをクリックしたくない。また、あなたのコードを[mcve]に減らしてください。あなたが求めている質問に完全に無関係なコードがたくさんあります。 –
あなたは選択したコードを正しくフォーマットするための '{}'ボタンがあります。スクリーンショットではなく、問題のテキストとしてエラーメッセージを挿入します。 'Question.txt'をスクリーンショットではなく、問題のテキストとして入力してください。誰もそれをテストしてコピーすることはできません。 – furas