11import pandas as pd
22import random
33import tkinter as tk
4- from tkinter import filedialog , messagebox
4+ from tkinter import filedialog , messagebox , ttk
55
6- # Load questions from the second column and answers from the third column of the Excel sheet
7- def load_questions_and_answers (file_path ):
6+ # Load questions from the selected sheet in the Excel file
7+ def load_questions_from_sheet (file_path , selected_sheet ):
88 try :
9- df = pd .read_excel (file_path )
9+ df = pd .read_excel (file_path , sheet_name = selected_sheet )
1010 s_no = df .iloc [:, 0 ].tolist ()
1111 questions = df .iloc [:, 1 ].tolist ()
1212 answers = df .iloc [:, 2 ].tolist ()
@@ -28,48 +28,121 @@ def __init__(self, root):
2828 self .root = root
2929
3030 self .root .title ("Random Question" )
31+ self .root .state ("zoomed" )
3132
32- ##Windows System##
33- self .root .state ("zoomed" ) # Maximizes the window
34-
35- self .original_questions = [] # Store the original order of questions
36- self .original_answers = [] # Store the original order of answers
37- self .questions = [] # Store the shuffled questions
38- self .answers = [] # Store the shuffled answers
39- self .current_question_index = - 1 # Initialize to -1 to start with the first question
33+ self .original_questions = []
34+ self .original_answers = []
35+ self .questions = []
36+ self .answers = []
37+ self .current_question_index = - 1
4038 self .remaining_label = tk .Label (root , text = "" , font = ("Ubuntu Mono" , 14 ))
4139 self .remaining_label .pack ()
4240 self .question_display = tk .Label (root , text = """:::: Interview Preparation ::::
43- ** Created By :: Vigneshkumar ** \U0001F60E """ , font = ("Ubuntu Mono" , 24 ),
44- wraplength = 900 )
41+ \U0001F60E """ , font = ("Ubuntu Mono" , 24 ), wraplength = 900 )
4542 self .question_display .pack (expand = True )
4643
47- # Create a frame to hold the buttons
4844 button_frame = tk .Frame (root )
4945 button_frame .pack ()
5046
51- self .instructions_button = tk .Button (button_frame , text = "Instructions" , command = self .display_instructions ,
52- font = ("Ubuntu Mono" , 18 ))
53- self .instructions_button .pack (side = "left" )
47+ # Upload File button
48+ self .upload_file_button = tk .Button (
49+ button_frame ,
50+ text = "Upload File" ,
51+ command = self .upload_and_populate_sheets , # Updated command
52+ font = ("Ubuntu Mono" , 18 )
53+ )
54+ self .upload_file_button .pack (side = "left" )
55+
56+ # Dropdown menu to select the sheet
57+ self .sheet_var = tk .StringVar ()
58+ self .sheet_dropdown = ttk .Combobox (
59+ button_frame ,
60+ textvariable = self .sheet_var ,
61+ values = [],
62+ state = 'readonly'
63+ )
64+ self .sheet_dropdown .pack (side = "left" )
65+
66+ # Select Sheet button
67+ self .select_sheet_button = tk .Button (
68+ button_frame ,
69+ text = "Select Sheet" ,
70+ command = self .load_questions_from_selected_sheet , # New method for selecting and loading sheet
71+ font = ("Ubuntu Mono" , 18 )
72+ )
73+ self .select_sheet_button .pack (side = "left" )
74+
75+ # Next Question
76+ self .next_button = tk .Button (
77+ button_frame ,
78+ text = "Next Question" ,
79+ command = self .next_question ,
80+ font = ("Ubuntu Mono" , 18 ),
81+ bg = "#FF5733"
82+ )
83+ self .next_button .pack (side = "left" )
5484
55- self .load_button = tk .Button (button_frame , text = "Upload Questions" , command = self .load_questions_from_file ,
56- font = ("Ubuntu Mono" , 18 ))
57- self .load_button .pack (side = "left" )
85+ # Show Answer
86+ self .show_answer_button = tk .Button (
87+ button_frame ,
88+ text = "Show Answer" ,
89+ command = self .show_answer ,
90+ font = ("Ubuntu Mono" , 18 ),
91+ bg = "#7AB7F5"
92+ )
93+ self .show_answer_button .pack (side = "left" )
5894
59- self .reset_button = tk .Button (button_frame , text = "Reset" , command = self .reset , font = ("Ubuntu Mono" , 18 ))
95+ # Reset
96+ self .reset_button = tk .Button (
97+ button_frame , text = "Reset" , command = self .reset , font = ("Ubuntu Mono" , 18 )
98+ )
6099 self .reset_button .pack (side = "left" )
61100
62- self .next_button = tk .Button (button_frame , text = "Next Question" , command = self .next_question ,
63- font = ("Ubuntu Mono" , 18 ))
64- self .next_button .pack (side = "left" )
101+ # Instructions
102+ self .instructions_button = tk .Button (
103+ button_frame , text = "Instructions" , command = self .display_instructions ,
104+ font = ("Ubuntu Mono" , 18 )
105+ )
106+ self .instructions_button .pack (side = "left" )
65107
66- self .show_answer_button = tk .Button (button_frame , text = "Show Answer" , command = self .show_answer ,
67- font = ("Ubuntu Mono" , 18 ))
68- self .show_answer_button .pack (side = "left" )
108+ # About
109+ self .about_button = tk .Button (
110+ button_frame , text = "About" , command = self .about_content ,
111+ font = ("Ubuntu Mono" , 18 )
112+ )
113+ self .about_button .pack (side = "left" )
69114
70115 self .answer_display = tk .Label (root , text = "" , font = ("Ubuntu Mono" , 18 ), wraplength = 900 )
71116 self .answer_display .pack (expand = True )
72117
118+ def upload_and_populate_sheets (self ):
119+ self .file_path = filedialog .askopenfilename (filetypes = [("Excel Files" , "*.xlsx" )])
120+
121+ if self .file_path :
122+ excel_file = pd .ExcelFile (self .file_path )
123+ self .sheet_dropdown ['values' ] = excel_file .sheet_names
124+ self .sheet_var .set ("" ) # Clear the selected sheet
125+ self .original_questions = []
126+ self .original_answers = []
127+ self .questions = []
128+ self .answers = []
129+ self .current_question_index = - 1
130+ self .display_question ()
131+ else :
132+ messagebox .showinfo ("Alert" , "Please upload an Excel file." )
133+
134+ def about_content (self ):
135+ aboutt = """
136+ About This Tool:
137+ This tool is designed to help you prepare for interviews by providing a random selection of questions and answers.
138+ Simply upload an Excel sheet with questions and answers, and start practicing!
139+
140+ Enjoy your interview preparation!
141+ Github Link = https://github.com/mechtester/Random-Question-Generator
142+ ** Created By :: Vigneshkumar **
143+ """
144+ messagebox .showinfo ("About" , aboutt )
145+
73146 def display_instructions (self ):
74147 instructions = """
75148 Instructions:
@@ -81,15 +154,28 @@ def display_instructions(self):
81154 """
82155 messagebox .showinfo ("Instructions" , instructions )
83156
84- def load_questions_from_file (self ):
85- file_path = filedialog .askopenfilename (filetypes = [("Excel Files" , "*.xlsx" )])
86- if file_path :
87- self .original_questions , self .original_answers = load_questions_and_answers (file_path )
157+ def load_questions_from_selected_sheet (self ):
158+ selected_sheet = self .sheet_var .get ()
159+
160+ if not selected_sheet :
161+ messagebox .showinfo ("Alert" , "Please select a sheet name." )
162+ return
163+
164+ if not self .file_path :
165+ messagebox .showinfo ("Alert" , "Please upload an Excel file first." )
166+ return
167+
168+ # Use the selected sheet name to load questions from the existing Excel file
169+ self .original_questions , self .original_answers = load_questions_from_sheet (self .file_path , selected_sheet )
170+
171+ if self .original_questions :
172+ self .answers = [] # Reset the answers list
173+ self .answer_display .config (text = "" ) # Clear the answer display
88174 self .shuffle_questions_and_answers ()
89175 self .current_question_index = - 1
90176 self .display_question ()
91177 else :
92- messagebox .showinfo ("Alert" , "Please upload a question file ." )
178+ messagebox .showinfo ("Alert" , "No questions found in the selected sheet ." )
93179
94180 def shuffle_questions_and_answers (self ):
95181 # Shuffle the questions and answers while maintaining their correspondence
@@ -162,8 +248,3 @@ def show_answer(self):
162248 root = tk .Tk ()
163249 app = QuestionApp (root )
164250 root .mainloop ()
165-
166-
167-
168-
169-
0 commit comments