6

私はPythonが初めてで、現在tkinterを使用してフォームを設計しようとしています。質問に対する答えが見つからないため、スクロールバーとフォームをノートブックに挿入しようとして立ち往生しています。「ノートブック tkinter ウィジェットにスクロールバーとフォームを挿入するにはどうすればよいですか?」...ご覧のとおり、あなたにとっては簡単ですが、私のような初心者にとってはそうではありません!

ただし、これは私がこれまで行ってきたことです。幸いなことに、スクロールバーは表示されますが、フォームをノートブックに挿入しようとするとクラッシュします!

注: 私の python バージョンは、EPD_free 7.3-2 (32 ビット) を使用した Python 2.7.3 です。

import Tkinter
from Tkinter import * 
from ttk import *   
import tkMessageBox 
import ttk  
import Tkinter as tk 

root = Tk()
root.title("Model_A")
root.resizable(0,0)

# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None

#Child Frames
ContainerOne = Frame(notebook)
ContainerOne.pack(fill=BOTH, expand=True)
ContainerTwo = Frame(notebook)
ContainerTwo.pack(fill=BOTH, expand=True)
ContainerThree = Frame(notebook)
ContainerThree.pack(fill=BOTH, expand=True)
ContainerFour = Tkinter.Frame(notebook)
ContainerFour.pack(fill=BOTH, expand=True)

#Create the pages
notebook.add(ContainerOne, text='Mode A')
notebook.add(ContainerTwo, text='Mode B')
notebook.add(ContainerThree, text='Mode C')
notebook.add(ContainerFour, text='Mode D')

canvas = Canvas(ContainerOne, width=200, height=400)
scroll = Scrollbar(ContainerOne, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerTwo, width=200, height=400)
scroll = Scrollbar(ContainerTwo, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)

canvas = Canvas(ContainerThree, width=200, height=400)
scroll = Scrollbar(ContainerThree, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerFour, width=200, height=400)
scroll = Scrollbar(ContainerFour, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
frame = Frame(canvas, width=200, height=1000)
canvas.create_window(100, 500, window=frame)

frameOne = None  

def defocus(event):
    event.widget.master.focus_set()    
    if __name__ == '__main__':
        ContainerOne= Tkinter.Label(notebook, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
        frameOne.grid(row=2, columnspan=7, sticky='W', \
                 padx=5, pady=5, ipadx=5, ipady=5)

#Component Selection
componentComb= ttk.Combobox(ContainerOne, width="19")
componentComb = Combobox(ContainerOne, state="readonly", values=("A", "B", "C"))
componentComb.grid(column=4, row=4, columnspan="5", sticky="nswe")
componentComb.set("Main Selection")

root.mainloop()
4

2 に答える 2

9

ノートブック ウィジェットのオプションを見ると、yviewも もyscrollcommand存在しないことがわかります。さらに、Frame ウィジェットもスクロールできません。

できることは、 内に Scrollbar を持つ Canvas ウィジェットを作成し、frameOne次に Frame を使用して Canvas に追加することcreate_windowです。

root = Tk()
root.resizable(0,0)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None
container = Frame(notebook)
container.pack(fill=BOTH, expand=True)
notebook.add(container, text='Mode A')

canvas = Canvas(container, width=200, height=400)
scroll = Scrollbar(container, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)

frame = Frame(canvas, bg='white', width=200, height=1000)
canvas.create_window(100, 500, window=frame)

root.mainloop()
于 2013-04-04T09:18:38.893 に答える
4

Pythonとtkinterがフォームをノートブックとスクロールバーに挿入する際の問題を解決しました。@A.Rodas の親切な助けに感謝します。

これは私のコードです。お役に立てば幸いです。

import Tkinter
from Tkinter import *
from ttk import *
import tkMessageBox
import math 
import ttk 
import Tkinter as tk

def defocus(event):
    event.widget.master.focus_set()

# start of GUI code
root = Tk()
root.title("Model A")
root.resizable(0,0)

# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None

# Child frames
ContainerOne = Frame(notebook)
ContainerOne.pack(fill=BOTH, expand=True)
ContainerTwo = Frame(notebook)
ContainerTwo.pack(fill=BOTH, expand=True)

# Create the pages
notebook.add(ContainerOne, text='Mode A')
notebook.add(ContainerTwo, text='Mode B')

canvas1 = Canvas(ContainerOne, width=1200, height=450)
scroll = Scrollbar(ContainerOne, command=canvas1.yview)
canvas1.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas1.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas2 = Canvas(ContainerTwo, width=1200, height=450)
scroll = Scrollbar(ContainerTwo, command=canvas2.yview)
canvas2.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas2.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)

frameOne = Frame(canvas1, width=800, height=450)
canvas1.create_window(250, 125, window=frameOne)

frameTwo = Frame(canvas2, width=800, height=450)
canvas2.create_window(200, 140, window=frameTwo)

# Main Frame

#Close Application Button
def quit(root):
    root.destroy()

ttk.Button(root, text="Close Application", command=lambda root=root:quit(root)).pack()

if __name__ == '__main__':
    #Main Part
    stepOne = Tkinter.LabelFrame(frameOne, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
    stepOne.grid(row=0, columnspan=5, sticky='nsew', padx=5, pady=5, ipadx=5, ipady=5)
    stepTwo = Tkinter.LabelFrame(frameOne, text=" 2. Calculate AP : ", font=("fixedsys", "16","bold italic"))
    stepTwo.grid(row=2, columnspan=7, sticky='WE', padx=5, pady=5, ipadx=5, ipady=5)

# First Step Starts 

# Component Selection
componentComb= ttk.Combobox(stepOne, width="19")
componentComb = Combobox(stepOne, state="readonly", values=("CDA", "VHS", "DVD"))
componentComb.grid(column=4, row=0, columnspan="5", sticky="nswe")
componentComb.set("Component Selection")

# Temperature Selection
tempComb = ttk.Combobox(stepOne, width="12")
tempComb = Combobox(stepOne, state="readonly", values=("-40", "-30", "-20","-10", "0",))
tempComb.grid(column=0, row=2, columnspan="2", sticky="w")
tempComb.set("Temperature Selection")

# Second Step Starts
inEncLbl = Tkinter.Label(stepTwo, text="Oxide:")
inEncLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2)

inEncTxt = Tkinter.Entry(stepTwo, width=6)
inEncTxt.grid(row=2, column=1, sticky='w', pady=2)

outEncLbl = Tkinter.Label(stepTwo, text="Density Rate (DR):")
outEncLbl.grid(row=2, column=5, padx=5, pady=2)

outEncTxt = Tkinter.Entry(stepTwo, width=6)
outEncTxt.grid(row=2, column=7,sticky='w', pady=2)

#End Code
root.mainloop()
于 2013-04-10T10:43:54.477 に答える