Skip to content

Commit fce2f92

Browse files
authored
Update 01_linux_script.py
Updated code for select individual sheet
1 parent 551cab8 commit fce2f92

File tree

1 file changed

+94
-42
lines changed

1 file changed

+94
-42
lines changed

01_linux_script.py

Lines changed: 94 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import pandas as pd
22
import random
33
import 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,63 +28,109 @@ def __init__(self, root):
2828
self.root = root
2929

3030
self.root.title("Random Question")
31-
self.root.attributes("-zoomed", True) # Start in maximized (full-screen) mode
31+
self.root.attributes("-zoomed", True)
3232

33-
self.original_questions = [] # Store the original order of questions
34-
self.original_answers = [] # Store the original order of answers
35-
self.questions = [] # Store the shuffled questions
36-
self.answers = [] # Store the shuffled answers
37-
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
3838
self.remaining_label = tk.Label(root, text="", font=("Ubuntu Mono", 14))
3939
self.remaining_label.pack()
4040
self.question_display = tk.Label(root, text=""":::: Interview Preparation ::::
41-
\U0001F60E""", font=("Ubuntu Mono", 24),
42-
wraplength=900)
41+
\U0001F60E""", font=("Ubuntu Mono", 24), wraplength=900)
4342
self.question_display.pack(expand=True)
4443

45-
# Create a frame to hold the buttons
4644
button_frame = tk.Frame(root)
4745
button_frame.pack()
4846

49-
#Instructions
50-
self.instructions_button = tk.Button(button_frame, text="Instructions", command=self.display_instructions,
51-
font=("Ubuntu Mono", 18))
52-
self.instructions_button.pack(side="left")
53-
#Upload Questions
54-
self.load_button = tk.Button(button_frame, text="Upload Questions", command=self.load_questions_from_file,
55-
font=("Ubuntu Mono", 18))
56-
self.load_button.pack(side="left")
57-
#Reset
58-
self.reset_button = tk.Button(button_frame, text="Reset", command=self.reset, font=("Ubuntu Mono", 18))
59-
self.reset_button.pack(side="left")
60-
#Next Question
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
6176
self.next_button = tk.Button(
6277
button_frame,
6378
text="Next Question",
6479
command=self.next_question,
6580
font=("Ubuntu Mono", 18),
66-
bg="#FF5733" # Replace with your desired color code
81+
bg="#FF5733"
6782
)
6883
self.next_button.pack(side="left")
6984

70-
#Show Answer
85+
# Show Answer
7186
self.show_answer_button = tk.Button(
7287
button_frame,
7388
text="Show Answer",
7489
command=self.show_answer,
7590
font=("Ubuntu Mono", 18),
76-
bg="#7AB7F5" # Replace with your desired color code
91+
bg="#7AB7F5"
7792
)
7893
self.show_answer_button.pack(side="left")
7994

95+
# Reset
96+
self.reset_button = tk.Button(
97+
button_frame, text="Reset", command=self.reset, font=("Ubuntu Mono", 18)
98+
)
99+
self.reset_button.pack(side="left")
100+
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")
107+
80108
# About
81-
self.about_button = tk.Button(button_frame, text="About", command=self.about_content,
82-
font=("Ubuntu Mono", 18))
109+
self.about_button = tk.Button(
110+
button_frame, text="About", command=self.about_content,
111+
font=("Ubuntu Mono", 18)
112+
)
83113
self.about_button.pack(side="left")
84114

85115
self.answer_display = tk.Label(root, text="", font=("Ubuntu Mono", 18), wraplength=900)
86116
self.answer_display.pack(expand=True)
87117

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+
88134
def about_content(self):
89135
aboutt = """
90136
About This Tool:
@@ -97,8 +143,6 @@ def about_content(self):
97143
"""
98144
messagebox.showinfo("About", aboutt)
99145

100-
101-
102146
def display_instructions(self):
103147
instructions = """
104148
Instructions:
@@ -110,15 +154,28 @@ def display_instructions(self):
110154
"""
111155
messagebox.showinfo("Instructions", instructions)
112156

113-
def load_questions_from_file(self):
114-
file_path = filedialog.askopenfilename(filetypes=[("Excel Files", "*.xlsx")])
115-
if file_path:
116-
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
117174
self.shuffle_questions_and_answers()
118175
self.current_question_index = -1
119176
self.display_question()
120177
else:
121-
messagebox.showinfo("Alert", "Please upload a question file.")
178+
messagebox.showinfo("Alert", "No questions found in the selected sheet.")
122179

123180
def shuffle_questions_and_answers(self):
124181
# Shuffle the questions and answers while maintaining their correspondence
@@ -191,8 +248,3 @@ def show_answer(self):
191248
root = tk.Tk()
192249
app = QuestionApp(root)
193250
root.mainloop()
194-
195-
196-
197-
198-

0 commit comments

Comments
 (0)