Python Forum
[Tkinter] Help with tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Help with tkinter
#1
It doesn't want to update my window... Wall

from tkinter import *
from tkinter.messagebox import *
from tkinter.ttk import *

state = 0

def one(ans, mainapp):
    global state
    if ans=="stolons":
        showinfo("Good answer!", "Good answer!")
        state+=1

mainapp = Tk()
mainapp.update()
mainapp.title("Questionnaire SVT")
l = LabelFrame(mainapp, text="Title")
l.pack(padx=20, pady=10)
if state==0:
    Label(l, text="Question 1:").pack()
    Label(l, text="question: uheiufzeds?").pack(pady=10)
    answer=Entry(l)
    answer.pack()
    Button(l, text="Ok", command=lambda: one(answer.get(), mainapp)).pack()
if state==1:
    Label(l, text="Question 2:").pack()
    
Label(mainapp, text=";D").pack()
mainapp.mainloop()
Reply
#2
It is not advisable to use * imports see this thread
It is advisable to use classes when doing GUI programs.

What doesn't want to update your window ?

If you mean it doesn't do the if state==1: condition, this will only be read the one time before it starts the GUI mainloop.
Even if this was not GUI code theres no looping to re-ceck the value of state.
GUI event driven code has to be written differently then normal code logic a loop would not work.
Reply
#3
Ok thx i will se what i can do :/ Thumbs Up
Reply
#4
Here is an example of using classes and event driven code
import tkinter as tk
from itertools import count
from tkinter import messagebox


class QuestionFrame(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title('Questionnaire SVT')
        label_frame = tk.LabelFrame(self, text="Title")
        label_frame.pack(padx=20, pady=10)
        self.question_no_label = tk.Label(label_frame)
        self.question_no_label.pack()
        self.question_label = tk.Label(label_frame)
        self.question_label.pack(pady=10)
        self.answer_entry = tk.Entry(label_frame)
        self.answer_entry.pack()
        self.ok_button = tk.Button(
            label_frame, text="Ok", command=self.on_ok_button)
        self.ok_button.pack()
        tk.Label(self, text=";D").pack()
        self.create_questions()

    def show_question(self, question_data):
        self.question_no_label['text'] = f'Question {question_data.number}:'
        self.question_label['text'] = f'Question: {question_data.question}'

    def create_questions(self):
        self.questions = [QuestionData('first question', 'first answer'),
                          QuestionData('second question', 'second answer')]
        self.question_number = 0
        self.show_current_question()

    def get_current_question(self):
        return self.questions[self.question_number]

    def show_current_question(self):
        self.show_question(self.get_current_question())

    def on_ok_button(self):
        question = self.get_current_question()
        answer = self.answer_entry.get()
        if answer == question.answer:
            messagebox.showinfo(
                'Well Done', f'{answer} is the Correct answer')
            self.show_next_question()
        else:
            messagebox.showinfo('Unlucky', f'{answer} is the Wrong answer')

        self.answer_entry.delete(0, tk.END)

    def show_next_question(self):
        if len(self.questions)-1 == self.question_number:
            messagebox.showinfo('Game over', 'No more questions')
        else:
            self.question_number += 1
        self.show_current_question()


class QuestionData:
    number = count(1)

    def __init__(self, question, answer):
        self.question = question
        self.answer = answer
        self.number = next(QuestionData.number)


if __name__ == "__main__":
    question_frame = QuestionFrame()
    question_frame.mainloop())
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020